I am trying to get user activity states by ActivityRecognition
API and it works in foreground and for few hours in background too (When user exit the app) but after more than 4/5 hours it stop sending user activity states by ActivityRecognition
API.
Setup Activity Recognition and Registering BroadcastReceiver In Main Activity
public static void ActivityRecognitionSetup() {
final String TRANSITIONS_RECEIVER_ACTION = "com.example.TRANSITIONS_RECEIVER_ACTION";
Intent mintent = new Intent(TRANSITIONS_RECEIVER_ACTION);
ActivityRecognitionClient client = ActivityRecognition.getClient(context);
PendingIntent pendingIntent;
if (App_Functions.runningSOrLater){
pendingIntent = PendingIntent.getBroadcast(Application.getmContext(), 255, mintent, PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_MUTABLE);
}else {
pendingIntent = PendingIntent.getBroadcast(MyApplication.getmContext(), 255, mintent, PendingIntent.FLAG_UPDATE_CURRENT);
}
ActivityRecognitionBroadcastReceiver broadcastReceiver = new ActivityRecognitionBroadcastReceiver();
Task<Void> task = client.requestActivityUpdates(6 * 60 * 60 * 1000, pendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Log.d(TAG, "onSuccess: Activity recognition Started");
context.registerReceiver(broadcastReceiver, new IntentFilter(TRANSITIONS_RECEIVER_ACTION));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: Activity recognition Failed");
}
});
}
Also Registering BR in Manifest
<receiver
android:name=".ActivityRecognitionBroadcastReceiver"
android:exported="false"
android:permission="com.google.android.gms.permission.ACTIVITY_RECOGNITION">
<intent-filter>
<action android:name="com.example.TRANSITIONS_RECEIVER_ACTION"/>
</intent-filter>
</receiver>
Broadcast Receiver
public class ActivityRecognitionBroadcastReceiver extends BroadcastReceiver {
final String TRANSITIONS_RECEIVER_ACTION ="com.example.TRANSITIONS_RECEIVER_ACTION";
@Override
public void onReceive(Context context, Intent intent) {
if (!TextUtils.equals(TRANSITIONS_RECEIVER_ACTION, intent.getAction())) {
return;
}
if (ActivityRecognitionResult.hasResult(intent)) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
// This Method Will Show Notification On Every Activity Recognition Detection
showNotification("Activity Recognition "+activity.getConfidence(),result.getProbableActivities())
}else{
Log.d(TAG, "ActivityRecognition Has NO Result "+ActivityRecognitionResult.hasResult(intent));
}
}
}
Even on idle the device is getting notifications but after 4-5 hours or sometimes more it stop receiving any broadcast from activity recognition api. Any ideas why this happening?