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

2023-09-06 02:32:51 作者:最美不过四叶草

我想注册一个接收器,用于去除SD卡的,但我的接收机是没有得到所谓的拆除SD卡粘贴在这里我code的。我注册在OnCreate(接收器),并注销了的OnDestroy函数。请让我知道,如果我做的任何错误。

I am trying to register a receiver for the removal of the sdcard, but my receiver is not getting called on removal of the sd card pasting my code here. I am registering the receiver in the oncreate() and unregistering in the ondestroy function. Please let me know if i am doing any mistake.

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);
}

请让我知道什么是错误的,我code。

Please let me know if anything is wrong in my code.

推荐答案

尝试添加到您的意图过滤器

Try adding this to your intent-filter

filter.addDataScheme("file");

看来你正试图赶上发送路径的意图的数据字段的操作。如果是这样的话,那么你的意图过滤器必须具备的匹配方案。

It appears the actions you are trying to catch send the path as the data field of the intent. If that is the case, then your intent filter must have the matching scheme.

最后,这只是一个建议,但我会建议你在Intent类中使用手动输入了你的行动的常数代替。 IE浏览器,直接使用使用字符串Intent.ACTION_MEDIA_REMOVED的而不是你。

Finally, and this is just a suggestion, but I would recommend you use the constants in the Intent class instead of typing out your actions manually. IE, use Intent.ACTION_MEDIA_REMOVED instead of you using the string directly.