監(jiān)聽外置sdcard(TF卡)的插拔事件
在編碼的過程中需要監(jiān)聽外置sdcard的插拔事件艇潭,在AndroidManifest.xml
中注冊一個(gè)靜態(tài)廣播
一定要添加上 <data android:scheme="file" />
<receiver android:name=".SdcardReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.MEDIA_UNMOUNTED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
在java代碼中繼承 BroadcastReceiver
,監(jiān)聽到sdcard的插拔事件讹开,然后做相應(yīng)的邏輯操作
public class SdcardReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_MEDIA_MOUNTED)){
Log.d("tag","sdcard mounted");
}else if(action.equals(Intent.ACTION_MEDIA_UNMOUNTED)){
Log.d("tag","sdcard unmounted");
}
}
}