Android 动态注册时未调用BroadcastReceiver onReceive()

Android 动态注册时未调用BroadcastReceiver onReceive(),android,android-intent,android-activity,google-glass,google-gdk,Android,Android Intent,Android Activity,Google Glass,Google Gdk,在清单中注册BroadcastReceiver时调用函数“onReceive”,但如果动态注册,则不调用函数 有效的代码如下所示: public class EyeGesture extends BroadcastReceiver { //Eye Gesture private static IntentFilter eyeGestureIntent; private static Context eyeGestureContext; private static

在清单中注册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>
另外,我不想从这个类扩展BroadcastReceiver。如果动态注册,为什么我没有收到任何东西。我还从清单中删除了以下行:

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

但它仍然不起作用。没有引发错误或异常。
我做错了什么?

在XE21.3中观看adb logcat,它看起来像com.google.android.glass.action.EYE\u手势意图从未点击事件总线;相反,它直接跳到com.google.glass.action.TAKE_PICTURE,这与相机按钮的意图相同。因此,看起来眼动API未经宣布就被删除了。

您使用的是明确的意图吗?动态注册的广播接收器似乎无法接收明确的意图。隐含意图起作用。 供参考:

如果问题不是明确的意图,但是如果您正在使用LocalBroadcastManager进行sendBroadcast,那么请确保registerReceiver也被调用为LocalBroadcastManager,而不是上下文
  • 接收方应扩展BroadcastReceiver类
  • 在清单中定义接收者
  • 在代码中(可能是onCreate),注册接收者
    • 创建接收器对象
    • 定义意图过滤器
    • 调用RegisterReceiver()传入接收方和意图筛选器

  • 尝试使用ApplicationContext而不是Activity

    Modyifing线路:

    eyeGestureContext = MainFunct.getCurrentContext();
    
    我会按以下顺序尝试:

  • eyeGestureContext=getApplicationContext()
  • eyeGestureContext=getApplication()
  • 如果上述方法无效,我将扩展应用程序并执行以下操作:

    public class MyExtendedApplication extends Application {
    
        private static MyExtendedApplication instance;
    
        public static MyExtendedApplication getInstance() {
            return instance;
        }
    }
    
    这在全球“android.net.conn.CONNECTIVITY\u CHANGE”广播中对我很有用

    Context c = MyExtendedApplication.getInstance();
    c.registerReceiver(
            connectivtyChangedReceiver,
            connectivityFilter);
    

    因此,使用“com.google.android.glass.action.EYE_-signature”也应该如此,如果通过清单进行注册,它会起作用,但如果使用Context.registerReceiver(new BroadcastReceiver()动态注册,则不会起作用;这不是问题。感谢您的帮助动态注册时,您在LogCat中得到了什么输出?我也有同样的问题。似乎没有人有明确的答案。这对OP有什么帮助?他们发布了特定的代码。
    Context c = MyExtendedApplication.getInstance();
    c.registerReceiver(
            connectivtyChangedReceiver,
            connectivityFilter);