读书人

经过广播检测sdcard插拔操作

发布时间: 2012-06-26 10:04:13 作者: rapoo

通过广播检测sdcard插拔操作

public class TestSdcard extends Activity {@Overrideprotected void onDestroy() {super.onDestroy();//退出的时候需要取消广播接收this.unregisterReceiver(in);}DetectSdcard in;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);in = new DetectSdcard();IntentFilter intentf = new IntentFilter();intentf.addAction(Intent.ACTION_MEDIA_MOUNTED);intentf.addAction(Intent.ACTION_MEDIA_UNMOUNTED);//隐式intent需要加上下面这句作匹配,否则接收不到广播intentf.addDataScheme("file");  this.registerReceiver(in, intentf);}// 检测sdcard是否在机器中public class DetectSdcard extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if(intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){//挂载的...}else if(intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED)){//非挂载...}}}}

读书人网 >移动开发

热点推荐