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;
}
}
下面是Manifest文件
<?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>
但仍然不起作用。 没有错误或异常抛出。 我究竟做错了什么?
你是否使用明确的意图? 看起来,动态注册的广播接收器无法获得明确的意图。 隐含意图的工作。 供参考:http://streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html
如果问题不是明确的意图,但如果您使用LocalBroadcastManager作为sendBroadcast,那么请确保registerReceiver也被称为LocalBroadcastManager,而不是Context
尝试使用ApplicationContext而不是Activity。
主持人:
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_CHANGE”广播
Context c = MyExtendedApplication.getInstance();
c.registerReceiver(
connectivtyChangedReceiver,
connectivityFilter);
所以也应该为你的“com.google.android.glass.action.EYE_GESTURE”
在XE21.3中观看adb logcat,它看起来像com.google.android.glass.action.EYE_GESTURE意图从未击中事件总线; 相反,它会直接跳到com.google.glass.action.TAKE_PICTURE,这与照相机按钮具有相同的意图。 所以它看起来像眼睛的手势API没有通知就被移除了。
链接地址: http://www.djcxy.com/p/23757.html上一篇: BroadcastReceiver onReceive() not Called when registered dynamically