javaandroidbroadcastreceiveractivity-recognition

BroadcastReceiver in MainActivity does not receive Broadcast Intents


I'm quite new in Android programming so I may be making very dumb mistakes. I'm writing a simple app to collect activity data. I have a MainActivity and an ActivityRecognizedService to query Google's Activity Recognition APIs. I'm trying to receive the recognized activity from ActivityRecognizedService to the MainActivity so I can later write records periodically to a file along with other data I will be collecting. Problem is, the Thread I will use to write data on file is not receiving broadcast messages. This is a MWE of the project.

In: ActivityRecognizedService.java

public class ActivityRecognizedService extends IntentService {

    public static final String LOCAL_BROADCAST_NAME = new String("API.ACTIVITY.BROADCAST_NAME");

    public ActivityRecognizedService() {
        super("ActivityRecognizedService");
    }

    public ActivityRecognizedService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if (ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult activityRecognitionResult = ActivityRecognitionResult.extractResult(intent);
            Log.e("API.ACTIVITY.DETECTION", "Activity detected: " + activityRecognitionResult.getMostProbableActivity().toString());
            /* This DOES log the activity when detected */
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        }
    }
}

In MainActivity.java

public class MainActivity
        extends AppCompatActivity 
        implements GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener {
    private ActivityRecognitionResult activityRecognitionResult;
    private GoogleApiClient googleApiClient;
    private BroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstance) {
         super.onCreate(savedInstance);
         setContentView(R.layout.activity_main);

         /* check permissions, Google Play Services availabililty ... */

         googleApiClient = new GoogleApiClient.Builder(this);
                 .addApi(ActivityRecognition.API)
                 .addConnectionCallbacks(this)
                 .addOnConnectionFailedListener(this)
                 .build();

         broadcastReceiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
                 Log.d("API.ACTIVITY.RECEIVE", ActivityRecognitionResult.extractResult(intent).toString());
                 /* This DOES NOT log the activity result */
             }
         }

         googleApiClient.connect();

         // This is the thread I will use to write records on file, to do
         new Thread((Runnable) -> {
             LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter(ActivityRecognizedService.LOCAL_BROADCAST_NAME));
             while (true) {
                 SystemClock.sleep(250);
                 // do stuff..
             }
         }).start();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Intent intent = new Intent(this, ActivityRecognizedService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        ActivityRecognitionClient activityRecognitionClient = ActivityRecognition.getClient(this);
        Task task = activityRecognitionClient.requestActivityUpdates(250, pendingIntent);
}

}

Of course logging the received broadcast is not the final goal as I could leave it in the onHandleIntent(), instead I need to write the extracted result on file along with other data that I will collect in the last thread. I also have the registerReceiver call in the onResume() method.


Solution

  • I think the problem is you have broadcast the same incoming Intent . Ad its not have any action attached to it . So BroadcastReceiver you have registered with action "API.ACTIVITY.BROADCAST_NAME" will not get notified.

    Set the action to Intent.

    @Override
        protected void onHandleIntent(@Nullable Intent intent) {
            Intent sendIntent=new Intent(LOCAL_BROADCAST_NAME);
            LocalBroadcastManager.getInstance(this).sendBroadcast(sendIntent);
        }