首页 > 系统 > Android > 正文

39.android广播-监听sd卡

2019-11-09 15:27:04
字体:
来源:转载
供稿:网友

广播监听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();		}	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表