android – BroadcastReceiver onReceive()在动态注册时不调用

broadcastReceiver在Manifest中注册调用函数“onReceive”,但如果动态注册则不调用.

有效的代码如下:

public class EyeGesture extends broadcastReceiver {
    //Eye Gesture
    private static IntentFilter eyeGestureIntent;
    private static Context eyeGestureContext;
    private static StringBuilder gestureInfo = null;
    private static broadcastReceiver broadcastReceiver;

   // public void startEyeListening() {
        //Eye Gesture

    //}

    @Override
    public void onReceive(Context context,Intent intent) {
       // this = context;
        if (intent.getStringExtra("gesture").equals("WINK")) {
            Log.e("WINKED ","");
        }else {
            Log.e("SOMETHING","is detected " + intent.getStringExtra("gesture"));
        }
        //disable Camera Snapshot
       // abortbroadcast();

    }

    public void stopEyeListening() {
        eyeGestureContext.unregisterReceiver(broadcastReceiver);
        eyeGestureIntent = null;
        eyeGestureContext = null;
        gestureInfo = null;
    }

}

下面是清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.inno.inno.glassplugin" >

    <uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainFunct"
            android:icon="@drawable/ic_glass_logo"
            android:label="@string/title_activity_main_funct" >
            <intent-filter>
                <action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
            </intent-filter>
            <Meta-data
                android:name="com.google.android.glass.VoiceTrigger"
                android:resource="@xml/voice_trigger" />
        </activity>

        <receiver android:name="com.inno.inno.glassplugin.EyeGesture">
            <intent-filter>
                <action android:name="com.google.android.glass.action.EYE_GESTURE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

问题是动态注册时不会调用“onReceive”.我必须以动态的方式做到这一点.
下面是不工作代码代码.

public class EyeGesture extends Activity {
    //Eye Gesture
    IntentFilter eyeGestureIntentFilter;
    Context eyeGestureContext;
    broadcastReceiver broadcastReceiver;


    public  EyeGesture(){
        Log.e("CONSTRUCTOR ","");
        eyeGestureContext = MainFunct.getCurrentContext();
        eyeGestureIntentFilter = new IntentFilter("com.google.glass.action.EYE_GESTURE");
        eyeGestureIntentFilter.setPriority(1000);
        startRunning();
    }

    void startRunning(){
        eyeGestureContext.registerReceiver(new broadcastReceiver() {
            @Override
            public void onReceive(Context context,Intent intent) {
                Log.e("Received "," Something");
            }
        },eyeGestureIntentFilter);
    }


    @Override
    public  void onResume(){
        super.onResume();
    }

    @Override
    public  void  onPause(){
        super.onPause();
        unregisterReceiver(broadcastReceiver);
    }
    public void stopEyeListening() {
        eyeGestureContext.unregisterReceiver(broadcastReceiver);
        eyeGestureIntentFilter = null;
        eyeGestureContext = null;
    }

}

另外,我不想从这个类扩展broadcastReceiver.如果动态注册,为什么我没有收到任何内容.我还从Manifest中删除了以下行:

<receiver android:name="com.inno.inno.glassplugin.EyeGesture">
                <intent-filter>
                    <action android:name="com.google.android.glass.action.EYE_GESTURE" />
                </intent-filter>
 </receiver>

但仍然没有用.没有抛出错误或异常.
我究竟做错了什么?

解决方法

你使用明确的意图吗?似乎动态注册的广播接收器无法接收显式意图.隐含的意图工作.
供参考: http://streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html

如果问题不是显式意图,但如果您使用LocalbroadcastManager进行sendbroadcast,那么请确保registerReceiver也被称为LocalbroadcastManager而不是Context

相关文章

###实现效果*本实例主要实现用ViewPage和Fragment实现选项卡...
一、安装 JDK 下载JDK最新版本,下载地址如下: http://www....
这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...