my background service stops on android 8.1.0
MainClass:
private void startTracking() {
if (!isMyServiceRunning()) {
Intent intent = new Intent(MainActivity.this, BackgroundDetectedActivitiesService.class);
startService(intent);
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (BackgroundDetectedActivitiesService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
BackgroundDetectedActivitiesService:
public class BackgroundDetectedActivitiesService extends Service {
private static final String TAG = BackgroundDetectedActivitiesService.class.getSimpleName();
private Intent mIntentService;
private PendingIntent mPendingIntent;
private ActivityRecognitionClient mActivityRecognitionClient;
IBinder mBinder = new BackgroundDetectedActivitiesService.LocalBinder();
public class LocalBinder extends Binder {
public BackgroundDetectedActivitiesService getServerInstance() {
return BackgroundDetectedActivitiesService.this;
}
}
public BackgroundDetectedActivitiesService() {
}
@Override
public void onCreate() {
super.onCreate();
PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
mActivityRecognitionClient = new ActivityRecognitionClient(this);
mIntentService = new Intent(this, DetectedActivitiesIntentService.class);
mPendingIntent = PendingIntent.getService(this, 1, mIntentService, PendingIntent.FLAG_UPDATE_CURRENT);
requestActivityUpdatesButtonHandler();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
public void requestActivityUpdatesButtonHandler() {
Task<Void> task = mActivityRecognitionClient.requestActivityUpdates(
Constants.DETECTION_INTERVAL_IN_MILLISECONDS,
mPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Log.d("DTAG","Successfully requested activity updates");
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("DTAG","Requesting activity updates failed to start");
}
});
}
public void removeActivityUpdatesButtonHandler() {
Task<Void> task = mActivityRecognitionClient.removeActivityUpdates(
mPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Log.d("DTAG","Removed activity updates successfully!");
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("DTAG","Failed to remove activity updates!");
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
removeActivityUpdatesButtonHandler();
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.michlind.---">
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".DetectedActivitiesIntentService"
android:enabled="true"
android:exported="false" />
<service android:name=".BackgroundDetectedActivitiesService"></service>
<service android:name=".NotificationIntentService"></service>
</application>
</manifest>
I see my log "Removed activity updates successfully!" after couple of seconds
In Android 8 background services are not allowed. Instead of
context.startService()
you have to callcontext.startForegroundService()
. And then inonCreate()
of your service you must callstartForeground()
and provide a ongoing Notification to show the user your service is running.So,
onCreate()
should be as follows:The notification could be a normal ongoing notification. For example: