Saturday, 26 February 2022

BroadcastReceiver not receiving any activity recognition broadcast

My main activity registering broadcast for Activity Recognition but and don't see any calls to Broadcast Receiver's onReceive method. I am not able to figure out why it's not working as expected.

I am testing on Real device with android 11 (API 30) with all required permission

Main Activity Register Broadcast

List<ActivityTransition> transitions = new ArrayList<>();
        PendingIntent activityPendingIntent;


        // Action fired when transitions are triggered.
        final String TRANSITIONS_RECEIVER_ACTION = "com.example.TRANSITIONS_RECEIVER_ACTION";

        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.IN_VEHICLE)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                        .build());
        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.IN_VEHICLE)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                        .build());
        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.WALKING)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                        .build());
        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.WALKING)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                        .build());
        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.STILL)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                        .build());
        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.STILL)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                        .build());

        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.ON_BICYCLE)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)

                        .build());
        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.ON_BICYCLE)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                        .build());

        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.RUNNING)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                        .build());

        transitions.add(
                new ActivityTransition.Builder()
                        .setActivityType(DetectedActivity.RUNNING)
                        .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                        .build());


        ActivityTransitionRequest request = new ActivityTransitionRequest(transitions);
        Intent mintent = new Intent(this, ActivityRecognitionBroadcastReceiver.class);
        mintent.setAction(TRANSITIONS_RECEIVER_ACTION);

        activityPendingIntent = PendingIntent.getBroadcast(this,
                0,
                mintent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Task<Void> task = ActivityRecognition.getClient(this)
                .requestActivityTransitionUpdates(request, activityPendingIntent);


        task.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(@NonNull Void command) {
                checkFirstimeActivityRecognition = false;
                Log.i(TAG, "Successfully added transition updates listener");
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG,"Transitions could not be unregistered: " + e);
            }
        });
        ActivityRecognitionBroadcastReceiver broadcastReceiver = new ActivityRecognitionBroadcastReceiver();
        registerReceiver(broadcastReceiver, new IntentFilter(TRANSITIONS_RECEIVER_ACTION));
        Log.d(TAG, "setupActivityRecognition: Register Broadcast receiver ");

BroadcastReceiver

public class ActivityRecognitionBroadcastReceiver extends BroadcastReceiver {
    public static final String TAG = "Broadcast Recognition";
    
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "On Handle Intent");
        Log.d(TAG, "onReceive(): " + intent);
        Toast.makeText(MyApplication.getmContext(), intent.getAction(), Toast.LENGTH_SHORT).show();
       
    }

Manifest

<application ...

<receiver
            android:name=".Location_system.ActivityRecognitionBroadcastReceiver"
            android:permission="com.google.android.gms.permission.ACTIVITY_RECOGNITION">
            <intent-filter>
                <action android:name="com.example.TRANSITIONS_RECEIVER_ACTION"/>
            </intent-filter>
        </receiver>

</application>


from BroadcastReceiver not receiving any activity recognition broadcast

No comments:

Post a Comment