[英]Is a BroadcastReceiver's onReceive method always called the moment it gets registered dynamically?
[英]BroadcastReceiver onReceive() not Called when registered dynamically
當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
嘗試使用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_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被刪除而沒有公告。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.