[英]BroadcastReceiver onReceive() not Called when registered dynamically
The function "onReceive" is called when BroadcastReceiver is Registered in the Manifest but NOT called if registered dynamically. 当BroadcastReceiver在Manifest中注册时调用函数“onReceive”,但如果动态注册则不调用。
The code that works is below: 有效的代码如下:
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;
}
}
Below is the Manifest file 下面是清单文件
<?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>
The problem is that "onReceive" is NOT called when registered dynamically. 问题是动态注册时不会调用“onReceive”。 I have to do this in a dynamic way.
我必须以动态的方式做到这一点。 Below is the code that is NOT working code.
下面是不工作代码的代码。
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;
}
}
Also, I don't want to extend BroadcastReceiver from this class. 另外,我不想从这个类扩展BroadcastReceiver。 Why am I not receiving anything if registered dynamically.
如果动态注册,为什么我没有收到任何内容。 I also removed the following line from the Manifest:
我还从Manifest中删除了以下行:
<receiver android:name="com.inno.inno.glassplugin.EyeGesture">
<intent-filter>
<action android:name="com.google.android.glass.action.EYE_GESTURE" />
</intent-filter>
</receiver>
but still, it is not working. 但仍然没有用。 There is no error or exception thrown.
没有抛出错误或异常。 What am I doing wrong?
我究竟做错了什么?
Are you using explicit intent? 你使用明确的意图吗? It seems that dynamically registered broadcast receivers cannot receive explicit intents.
似乎动态注册的广播接收器无法接收显式意图。 Implicit intents work.
隐含的意图工作。 For reference: http://streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html
供参考: http : //streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html
If the issue is not explicit intents but if you are using LocalBroadcastManager for sendBroadcast then make sure that the registerReceiver is also called of LocalBroadcastManager and not of Context 如果问题不是显式意图,但是如果你使用LocalBroadcastManager进行sendBroadcast,那么确保registerReceiver也被称为LocalBroadcastManager而不是Context
Try using ApplicationContext instead of Activity. 尝试使用ApplicationContext而不是Activity。
Modyifing line: Modyifing线:
eyeGestureContext = MainFunct.getCurrentContext();
I would try things in this order: 我会按顺序尝试:
eyeGestureContext = getApplicationContext();
eyeGestureContext = getApplication();
If above does not work I would extend the Application and do: 如果以上不起作用,我会扩展应用程序并执行:
public class MyExtendedApplication extends Application {
private static MyExtendedApplication instance;
public static MyExtendedApplication getInstance() {
return instance;
}
}
This works for me with global "android.net.conn.CONNECTIVITY_CHANGE" broadcast 这适用于全球“android.net.conn.CONNECTIVITY_CHANGE”广播
Context c = MyExtendedApplication.getInstance();
c.registerReceiver(
connectivtyChangedReceiver,
connectivityFilter);
so should also for you with "com.google.android.glass.action.EYE_GESTURE" 你应该也可以使用“com.google.android.glass.action.EYE_GESTURE”
Watching adb logcat in XE21.3, it looks like com.google.android.glass.action.EYE_GESTURE intent never hits the event bus; 在XE21.3中观看adb logcat,看起来com.google.android.glass.action.EYE_GESTURE意图永远不会到达事件总线; instead, it skips straight to com.google.glass.action.TAKE_PICTURE, which is the same intent as the camera button.
相反,它直接跳到com.google.glass.action.TAKE_PICTURE,这与相机按钮的意图相同。 So it looks like the eye-gesture API was removed without announcement.
所以看起来像是手势API被删除而没有公告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.