没有得到SD卡相关的意图到我的广播接收器

时间:2011-06-09 13:54:18

标签: android sd-card

我正在尝试注册接收器以删除SD卡,但我的接收器没有被调用去除sd卡粘贴我的代码。我在oncreate()中注册接收器并在ondestroy函数中取消注册。如果我有任何错误,请告诉我。

void registerSDCardStateChangeListener() {
    final String MEDIA_REMOVED = "android.intent.action.MEDIA_REMOVED";
    final String MEDIA_UNMOUNTED = "android.intent.action.MEDIA_UNMOUNTED";
    final String MEDIA_BAD_REMOVAL = "android.intent.action.MEDIA_BAD_REMOVAL";
    //      final String MEDIA_EJECT = "android.intent.action.MEDIA_EJECT";
    final String MEDIA_EJECT = "android.intent.action.MEDIA_SCANNER_FINISHED";

    mSDCardStateChangeListener = new BroadcastReceiver() {

        @
        Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equalsIgnoreCase(MEDIA_REMOVED) || action.equalsIgnoreCase(MEDIA_UNMOUNTED) || action.equalsIgnoreCase(MEDIA_BAD_REMOVAL) || action.equalsIgnoreCase(MEDIA_EJECT)) {
                if (mMediaPlayer != null) {
                    stopPlayBack();
                }
            }
        }
    };

    IntentFilter filter = new IntentFilter();
    filter.addAction(MEDIA_REMOVED);
    filter.addAction(MEDIA_UNMOUNTED);
    filter.addAction(MEDIA_BAD_REMOVAL);
    filter.addAction(MEDIA_EJECT);
    registerReceiver(mSDCardStateChangeListener, filter);
}

如果我的代码中有任何问题,请告诉我。

3 个答案:

答案 0 :(得分:25)

尝试将此添加到您的intent-filter

filter.addDataScheme("file");

您尝试捕获的操作似乎将路径作为intent的数据字段发送。如果是这种情况,那么您的intent过滤器必须具有匹配方案。

最后,这只是一个建议,但我建议你使用Intent类中的常量,而不是手动输入你的动作。 IE,使用Intent.ACTION_MEDIA_REMOVED而不是直接使用字符串。

答案 1 :(得分:3)

Justin Breitfeller的回答对我有用,但作为一种替代技术,您也可以在AndroidManifest.xml中完成此操作:

<receiver android:enabled="true" android:exported="false"
  android:name="SDCardStateChangeListener">
  <intent-filter>
    <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
    <action android:name="android.intent.action.MEDIA_REMOVED" />
    <action android:name="android.intent.action.MEDIA_EJECT" />
    <action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
    <data android:scheme="file" />
  </intent-filter>
</receiver>

然后让接收器成为一个合适的类:

public class OnMediaMountedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action.equalsIgnoreCase(Intent.ACTION_MEDIA_REMOVED)
                || action.equalsIgnoreCase(Intent.ACTION_MEDIA_UNMOUNTED)
                || action.equalsIgnoreCase(Intent.ACTION_MEDIA_BAD_REMOVAL)
                || action.equalsIgnoreCase(Intent.ACTION_MEDIA_EJECT)) {
            if(mMediaPlayer != null) {
                stopPlayBack();
            }
        }               
    }
};

答案 2 :(得分:0)

我在app运行时找到了解决方案,用户卸载/删除了sdcard。

@Override
protected void onResume() {
    super.onStart();

    IntentFilter ejectFilter = new ntentFilter(Intent.ACTION_MEDIA_MOUNTED);
    ejectFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
    ejectFilter.addAction(Intent.ACTION_MEDIA_EJECT);
    ejectFilter.addDataScheme("file");
    registerReceiver(ejectReceiver, ejectFilter);

}

@Override
protected void onPause() {
    super.onPause();

    unregisterReceiver(ejectReceiver);
}

在课堂上,定义如下方法,

BroadcastReceiver ejectReceiver = new BroadcastReceiver() 
{               
      @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(Intent.ACTION_MEDIA_EJECT) || action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) 
        {
            Log.e("msg","Media Unmounted");

            //do something on unmounted sdcard from device
        }
    }
};

希望它会有所帮助。