您的位置:首页 > 移动开发 > Android开发

Android-监听SD卡状态(BroadcastReceiver)

2015-08-20 15:42 751 查看
注册广播接收者:

 <receiver android:name=".SDStatusReceiver">

     <intent-filter >

         <action android:name="android.intent.action.MEDIA_MOUNTED"/>

         <action android:name="android.intent.action.MEDIA_REMOVED"/>

         <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>

         <data android:scheme="file"/>

     </intent-filter>

</receiver>

SDStatusReceiver中获取广播

public class SDStatusReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
//判断收到的到底是什么广播
String action = intent.getAction();
if("android.intent.action.MEDIA_MOUNTED".equals(action)){
Toast.makeText(context, "SD卡可用", 0).show();
}
else if("android.intent.action.MEDIA_REMOVED".equals(action)){
Toast.makeText(context, "SD卡拔出", 0).show();
}
else if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
Toast.makeText(context, "SD卡不可用", 0).show();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息