动态注册 BroadcastReceiver 中的 onReceive() 方法未被调用

10

当BroadcastReceiver在清单文件中注册时,“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。如果动态注册,为什么我没有接收到任何内容?我也从清单中删除了以下行:
 <receiver android:name="com.inno.inno.glassplugin.EyeGesture">
                <intent-filter>
                    <action android:name="com.google.android.glass.action.EYE_GESTURE" />
                </intent-filter>
 </receiver>

但是,它仍然没有起作用。没有错误或异常抛出。 我做错了什么?


当动态注册时,您在LogCat中获得什么输出? - Aritra Roy
我也有同样的问题。看起来没有人有一个明确的答案。 - sprajagopal
4个回答

2
您是否正在使用显式意图?动态注册的广播接收器似乎无法接收显式意图。隐式意图可以使用。
参考链接:http://streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html 如果问题不是显式意图,而是您正在使用LocalBroadcastManager发送广播,请确保也调用了LocalBroadcastManager的registerReceiver方法,而不是Context的registerReceiver方法。

1

尝试使用ApplicationContext而不是Activity。

修改行:

eyeGestureContext = MainFunct.getCurrentContext();

我会按照以下顺序尝试:

  1. eyeGestureContext = getApplicationContext();
  2. eyeGestureContext = getApplication();

如果以上方法不起作用,我会扩展应用程序并执行以下操作:

public class MyExtendedApplication extends Application {

    private static MyExtendedApplication instance;

    public static MyExtendedApplication getInstance() {
        return instance;
    }
}

这对我来说很有效,使用全局的“android.net.conn.CONNECTIVITY_CHANGE”广播。
Context c = MyExtendedApplication.getInstance();
c.registerReceiver(
        connectivtyChangedReceiver,
        connectivityFilter);

所以对于你来说,也应该使用"com.google.android.glass.action.EYE_GESTURE"


0
在XE21.3版本中观察adb logcat,看起来com.google.android.glass.action.EYE_GESTURE意图从未触发事件总线;相反,它直接跳转到与相机按钮相同的com.google.glass.action.TAKE_PICTURE意图。因此,看起来眼部手势API已经被删除,而没有任何公告。

如果通过清单文件注册,它可以工作,但是如果使用Context.registerReceiver(new BroadcastReceiver())进行动态注册,则无法工作。这不是问题。感谢您的帮助。 - Programmer

-1
  • 接收器应该扩展BroadcastReceiver类。
  • 在清单文件中定义接收器
  • 在代码中(可能是onCreate方法中),注册接收器
    • 创建一个接收器对象
    • 定义意图过滤器
    • 调用RegisterReceiver()方法,传入接收器和意图过滤器

这对 OP 有什么帮助?他们发布了具体的代码。 - sprajagopal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接