Android5.0上SD卡抽拔发送的Intent研究
2015-07-31 14:05
417 查看
废话不多说,直接上代码吧!
package com.example.testsdcard;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
public class MainActivity extends Activity {
private IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 监听类private
final BroadcastReceiver sdcardListener = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
String action = arg1.getAction();
if (Intent.ACTION_MEDIA_BAD_REMOVAL.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_BAD_REMOVAL");
} else if (Intent.ACTION_MEDIA_CHECKING.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_CHECKING");
} else if (Intent.ACTION_MEDIA_EJECT.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_EJECT");
} else if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_MOUNTED");
} else if (Intent.ACTION_MEDIA_REMOVED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_REMOVED");
} else if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_SCANNER_FINISHED");
} else if (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_SCANNER_SCAN_FILE");
} else if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_SCANNER_STARTED");
} else if (Intent.ACTION_MEDIA_SHARED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_SHARED");
} else if (Intent.ACTION_MEDIA_UNMOUNTABLE.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_UNMOUNTABLE");
} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_UNMOUNTED");
} else if (Intent.ACTION_MEDIA_BUTTON.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_BUTTON");
} else if (Intent.ACTION_MEDIA_NOFS.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_NOFS");
}
}
};
intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
intentFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTABLE);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_BUTTON);
intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
intentFilter.addDataScheme("file");
registerReceiver(sdcardListener, intentFilter);
}
}
首先运行程序,在手机中插入SD卡(手机支持热插拔),查看log:
07-31 13:44:27.619: I/jiangguohu(2842): ACTION_MEDIA_CHECKING
07-31 13:44:28.970: I/jiangguohu(2842): ACTION_MEDIA_MOUNTED
07-31 13:44:29.681: I/jiangguohu(2842): ACTION_MEDIA_SCANNER_STARTED
07-31 13:44:33.034: I/jiangguohu(2842): ACTION_MEDIA_SCANNER_FINISHED可以看到,首先检查,然后挂载,再然后扫描。
再然后抽掉SD卡:
07-31 13:43:56.118: I/jiangguohu(2842): ACTION_MEDIA_UNMOUNTED
07-31 13:43:56.188: I/jiangguohu(2842): ACTION_MEDIA_BAD_REMOVAL
07-31 13:43:56.198: I/jiangguohu(2842): ACTION_MEDIA_EJECT
再之后,选择usb storage模式连接电脑(SD卡已经插入):
07-31 13:41:07.583: I/jiangguohu(2842): ACTION_MEDIA_EJECT
07-31 13:41:09.185: I/jiangguohu(2842): ACTION_MEDIA_UNMOUNTED
07-31 13:41:09.185: I/jiangguohu(2842): ACTION_MEDIA_UNMOUNTED
07-31 13:41:09.195: I/jiangguohu(2842): ACTION_MEDIA_SHARED
最后,取消usb storage模式:
07-31 13:41:39.835: I/jiangguohu(2842): ACTION_MEDIA_UNMOUNTED
07-31 13:41:39.855: I/jiangguohu(2842): ACTION_MEDIA_CHECKING
07-31 13:41:41.086: I/jiangguohu(2842): ACTION_MEDIA_MOUNTED
07-31 13:41:42.217: I/jiangguohu(2842): ACTION_MEDIA_SCANNER_STARTED
07-31 13:41:45.100: I/jiangguohu(2842): ACTION_MEDIA_SCANNER_FINISHED
综上,当SD卡没有挂载的时候 ACTION_MEDIA_EJECT 和 ACTION_MEDIA_UNMOUNTED 都会调用。当SD卡挂载的时候,调用 ACTION_MEDIA_CHECKING 和 ACTION_MEDIA_MOUNTED 。
package com.example.testsdcard;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
public class MainActivity extends Activity {
private IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 监听类private
final BroadcastReceiver sdcardListener = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
String action = arg1.getAction();
if (Intent.ACTION_MEDIA_BAD_REMOVAL.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_BAD_REMOVAL");
} else if (Intent.ACTION_MEDIA_CHECKING.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_CHECKING");
} else if (Intent.ACTION_MEDIA_EJECT.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_EJECT");
} else if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_MOUNTED");
} else if (Intent.ACTION_MEDIA_REMOVED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_REMOVED");
} else if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_SCANNER_FINISHED");
} else if (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_SCANNER_SCAN_FILE");
} else if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_SCANNER_STARTED");
} else if (Intent.ACTION_MEDIA_SHARED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_SHARED");
} else if (Intent.ACTION_MEDIA_UNMOUNTABLE.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_UNMOUNTABLE");
} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_UNMOUNTED");
} else if (Intent.ACTION_MEDIA_BUTTON.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_BUTTON");
} else if (Intent.ACTION_MEDIA_NOFS.equals(action)) {
android.util.Log.i("jiangguohu","ACTION_MEDIA_NOFS");
}
}
};
intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
intentFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTABLE);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_BUTTON);
intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
intentFilter.addDataScheme("file");
registerReceiver(sdcardListener, intentFilter);
}
}
首先运行程序,在手机中插入SD卡(手机支持热插拔),查看log:
07-31 13:44:27.619: I/jiangguohu(2842): ACTION_MEDIA_CHECKING
07-31 13:44:28.970: I/jiangguohu(2842): ACTION_MEDIA_MOUNTED
07-31 13:44:29.681: I/jiangguohu(2842): ACTION_MEDIA_SCANNER_STARTED
07-31 13:44:33.034: I/jiangguohu(2842): ACTION_MEDIA_SCANNER_FINISHED可以看到,首先检查,然后挂载,再然后扫描。
再然后抽掉SD卡:
07-31 13:43:56.118: I/jiangguohu(2842): ACTION_MEDIA_UNMOUNTED
07-31 13:43:56.188: I/jiangguohu(2842): ACTION_MEDIA_BAD_REMOVAL
07-31 13:43:56.198: I/jiangguohu(2842): ACTION_MEDIA_EJECT
再之后,选择usb storage模式连接电脑(SD卡已经插入):
07-31 13:41:07.583: I/jiangguohu(2842): ACTION_MEDIA_EJECT
07-31 13:41:09.185: I/jiangguohu(2842): ACTION_MEDIA_UNMOUNTED
07-31 13:41:09.185: I/jiangguohu(2842): ACTION_MEDIA_UNMOUNTED
07-31 13:41:09.195: I/jiangguohu(2842): ACTION_MEDIA_SHARED
最后,取消usb storage模式:
07-31 13:41:39.835: I/jiangguohu(2842): ACTION_MEDIA_UNMOUNTED
07-31 13:41:39.855: I/jiangguohu(2842): ACTION_MEDIA_CHECKING
07-31 13:41:41.086: I/jiangguohu(2842): ACTION_MEDIA_MOUNTED
07-31 13:41:42.217: I/jiangguohu(2842): ACTION_MEDIA_SCANNER_STARTED
07-31 13:41:45.100: I/jiangguohu(2842): ACTION_MEDIA_SCANNER_FINISHED
综上,当SD卡没有挂载的时候 ACTION_MEDIA_EJECT 和 ACTION_MEDIA_UNMOUNTED 都会调用。当SD卡挂载的时候,调用 ACTION_MEDIA_CHECKING 和 ACTION_MEDIA_MOUNTED 。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories