gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:41 28 4
gpt4 key购买 nike

当 BroadcastReceiver 在 Manifest 中注册时调用函数“onReceive”,但如果是动态注册则NOT调用。

有效的代码如下:

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;
}

}

下面是 list 文件

<?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。如果动态注册,为什么我没有收到任何东西。我还从 list 中删除了以下行:

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

但还是不行。没有错误或异常抛出。我做错了什么?

最佳答案

您是否使用了明确的 Intent ?似乎动态注册的广播接收器无法接收显式 Intent 。隐式 Intent 起作用。供引用:http://streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html

如果问题不是明确的 Intent ,但如果您将 LocalBroadcastManager 用于 sendBroadcast,那么请确保 registerReceiver 也被称为 LocalBroadcastManager 而不是 Context

关于android - 动态注册时未调用 BroadcastReceiver onReceive(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27387228/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com