我创建了一个BroadcastReceiver来检测SDCard的mount和unmount
事件,但是,我根本无法收到任何事件:
这里是 AndroidManifest.xml:
事件,但是,我根本无法收到任何事件:
这里是 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()); } }
解决方法
Intent javadoc指定了一个不同的动作:name值. 使用“android.intent.action.MEDIA_MOUNTED”而不是“android.content.Intent.ACTION_MEDIA_MOUNTED”