我创建了一个BroadcastReceiver来检测SDCard挂载和卸载 然而,事件,我根本无法接收任何事件: 这是AndroidManifest.xml:
<receiver android:enabled="true" android:label="SDCardMountReceiver" android:exported="true" android:name="xxx.broadcasts.SDCardBroadcastReceiver">
<intent-filter>
<action android:name="android.content.Intent.ACTION_MEDIA_MOUNTED"></action>
<!-- or <action android:name="android.content.Intent.ACTION_MEDIA_UNMOUNTED" />--></intent-filter>
</receiver>
和SDCardMountReceiver类:
public class SDCardBroadcastReceiver extends BroadcastReceiver {
public SDCardBroadcastReceiver() {
super();
System.err.println("constructor");
}
public void onReceive(Context context, Intent intent) {
Log.d("SDCardBroadCastReceiver", "receive " + intent.getAction());
System.err.println("jonathan receive " + intent.getAction());
}
}
答案 0 :(得分:45)
您还需要将数据方案设置为“file”。
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
答案 1 :(得分:4)
Intent javadoc指定了不同的操作:名称值。 使用“android.intent.action.MEDIA_MOUNTED”而不是“android.content.Intent.ACTION_MEDIA_MOUNTED”
答案 2 :(得分:4)
如果以编程方式注册广播接收器,则还必须将方案设置为“file”。
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addDataScheme("file");
mContext.registerReceiver(mExternalStorageReceiver, filter);