BroadcastReceiver onReceive () is not called during dynamic accounting

The onReceive function is called when the BroadcastReceiver is registered in the manifest, but NOT if it is registered dynamically.

Below is the code:

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 the onReceive NOT function is called during dynamic accounting. I have to do it dynamically. The code below is NOT a 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 the BroadcastReceiver from this class. Why am I not getting anything if registered dynamically. I also removed the following line from the 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 does not work. Bug fixed or exception. What am I doing wrong?

+7
android google-glass android-intent android-activity google-gdk
source share
4 answers

Are you using explicit intention? It seems that dynamically registered broadcast receivers cannot receive explicit intentions. Implicit intentions work. For reference: http://streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html

If the problem is not explicitly intended, but if you are using LocalBroadcastManager for sendBroadcast, make sure registerReceiver is also called by LocalBroadcastManager and not the context

+2
source share

Try using ApplicationContext instead of Activity.

Modifying line:

 eyeGestureContext = MainFunct.getCurrentContext(); 

I would try things in the following order:

  • eyeGestureContext = getApplicationContext();
  • eyeGestureContext = getApplication();

If the above does not work, I will expand the application and do:

 public class MyExtendedApplication extends Application { private static MyExtendedApplication instance; public static MyExtendedApplication getInstance() { return instance; } } 

This works for me with the global broadcast "android.net.conn.CONNECTIVITY_CHANGE"

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

so for you with "com.google.android.glass.action.EYE_GESTURE"

+1
source share

Looking at logbat adb in XE21.3, it looks like com.google.android.glass.action.EYE_GESTURE intent never gets on the event bus; instead, it goes straight to com.google.glass.action.TAKE_PICTURE, which is the same intent as the camera button. So it looks like the eye-gesture API has been removed without an announcement.

0
source share
  • The receiver must extend the BroadcastReceiver class.
  • Define the recipient in the manifest
  • In the code (possibly onCreate) register the receiver
    • Create Recipient Object
    • Define intent filters
    • call to RegisterReceiver () passing at the receiver and intent filters
0
source share

All Articles