广播监听sd卡,sd卡有三种状态MEDIA_MOUNTED===sd卡可用,MEDIA_REMOVED===sd卡拔出,MEDIA_UNMOUNTED===sd卡不可用,这里面不需要权限
清单文件(只需要和下面三种任意一个匹配就可以收到广播,data必须存在,为了匹配而存在):
<receiver android:name="com.ldw.sdlistener.sdReceiver"> <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>检测sd卡状态代码package com.ldw.sdlistener;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class sdReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //判断收到的是什么样的广播 String action = intent.getAction(); if("android.intent.action.MEDIA_MOUNTED".equals(action)){ Toast.makeText(context, "sd卡可用", Toast.LENGTH_SHORT).show(); } else if("android.intent.action.MEDIA_REMOVED".equals(action)){ Toast.makeText(context, "sd卡拔出", Toast.LENGTH_SHORT).show(); } else if("android.intent.action.MEDIA_UNMOUNTED".equals(object)){ Toast.makeText(context, "sd卡不可用", Toast.LENGTH_SHORT).show(); } }}
新闻热点
疑难解答