I am trying to get user activity states by Activity Recognition 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 or after 2 or 3 days it stop sending user activity states by Activity Recognition API. In specific REALME 12 and Samsung M12 Android 12 versions I am facing this inconsistency from last 2 weeks. When I debug and try to catch the issue then it will trigger and give me Activity Recognition update in on start command as I mention below. I also tried with Activity-Transition method and create new Broadcast receiver still getting no result at all. In Android version 14 it is working perfect.
Also Registering SERVICE in Manifest
<service
android:name="com.spectra.ess.service.TimeLineTrackingService"
android:exported="false"
android:foregroundServiceType="location" />
Permissions in Manifest
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
and also Runtime Permission for Physical Activity given.
Setup Activity Recognition and Registering Broadcast Receiver In TimeLineTrackingService
`public class TimeLineTrackingService extends Service {
@Override
public void onCreate() {
super.onCreate();
startActivityRecognitionUpdates();
}
private void startActivityRecognitionUpdates() {
Intent intent = new Intent(this, TimeLineTrackingService.class);
mActivityRecognitionPendingIntent = PendingIntent.getService(this, 100, intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mActivityRecognitionClient.requestActivityUpdates(ACTIVITY_RECOGNITION_DETECTION_INTERVAL,
mActivityRecognitionPendingIntent);
}
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (ActivityRecognitionResult.hasResult(intent)) {
final ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
// Run the activity detection logic in worker thread.
mHandler.post(new Runnable() {
public void run() {
handleActivityRecognition(result);
}
});
}
return START_STICKY;
}
}
malvi kiran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.