您的位置:首页 > 其它

SD卡监听---BroadcastReceiver

2016-05-11 16:51 120 查看
package com.demo.sdcard;

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) {
//判断收到的广播类型
//获取广播中的action
String action = intent.getAction();

if(Intent.ACTION_MEDIA_MOUNTED.equals(action)){
Toast.makeText(context, "sd卡就绪", 0).show();
}else if(Intent.ACTION_MEDIA_REMOVED.equals(action)){
Toast.makeText(context, "sd卡拔出", 0).show();
}else if(Intent.ACTION_MEDIA_UNMOUNTED.equals(action)){
Toast.makeText(context, "sd卡卸载", 0).show();
}
}

}
xml注册广播和添加sd卡状态
<pre name="code" class="java"><receiverandroid:name="com.demo.sdcard.SDReceiver"><intent-filter ><!-- 添加SD卡的状态    就绪,拔出,卸载 --><action android:name="android.intent.action.MEDIA_MOUNTED"/><action android:name="android.intent.action.MEDIA_REMOVED"/><action android:name="android.intent.action.MEDIA_UNMOUNTED"/></intent-filter></receiver>

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: