Sohaib
Sohaib

Reputation: 4694

Android service gets killed on swiping out the application

I have an Android Service class the code for which is as follows:

public class LoginService extends Service {
BroadcastReceiver wifiStateChangeReciever;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("AndroidLearning", "Service onStartCommand Started.");
    return Service.START_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i("AndroidLearning", "Service Started.");

    final IntentFilter intentFilter = new IntentFilter();
    // intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
    intentFilter.addAction("android.net.wifi.STATE_CHANGE");
    wifiStateChangeReciever = new WifiStateChangeReciever();
    this.registerReceiver(wifiStateChangeReciever, intentFilter, null, null);

    Log.i("AndroidLearning", "Reciever Registered.");
}

@Override
public void onDestroy() {
    Log.i("AndroidLearning", "Service Destroyed.");
    this.unregisterReceiver(wifiStateChangeReciever);
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Log.w("AndroidLearning", "On Task Remove: FLAG_STOP_WITH_TASK - "
            + ServiceInfo.FLAG_STOP_WITH_TASK);
    this.unregisterReceiver(wifiStateChangeReciever);
    Intent restartServiceIntent = new Intent(getApplicationContext(),
    this.getClass()); restartServiceIntent.setPackage(getPackageName());
    PendingIntent restartServicePendingIntent = PendingIntent.getService(
            getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager alarmService = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
    alarmService.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent); 
    Log.w("AndroidLearning", "End on task removed");
    super.onTaskRemoved(rootIntent);

}
}

It registers a BroadcastReciever. The Activity which starts this service has the following code:

Intent intent = new Intent(this, LoginService.class);
startService(intent);

However whenever the Activity is swiped out from the task list (recent) the service is also stopped. I over rode the onTaskRemoved to remedy it but it still does not seem to work and the AlarmManager never starts the pendingIntent. I have tries using both method: set and setExact for the AlarmManager.

I also tried adding the following options to <service> tags

android:stopWithTask="false"
android:process=":remote"

but to no avail.

What am I doing wrong here? Thanks for the help.

Upvotes: 4

Views: 5441

Answers (4)

android developer
android developer

Reputation: 116352

If you try this on some devices, sadly, it won't work.

Some OEMs decided to change the normal behavior of what happens when you remove an app from the recent tasks, so they become semi-disabled:

Upvotes: 1

Sohaib
Sohaib

Reputation: 4694

I finally found the answer to my own problem. It seems this was a problem with the particular flavor of android that I was running on my phone (Mi UI). There was a separate setting regarding each application whether it needed to be allowed to be restarted or not.

Screenshot - autostart is the setting that I was looking for

Unless this setting is configured no amount of changing permissions and setting Alarms helped me.

Upvotes: 5

Boban S.
Boban S.

Reputation: 1662

You need to start service in foreground if you don't want android to shut it down.

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
    System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
    getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);

http://developer.android.com/guide/components/services.html#Foreground

Upvotes: 1

MrJM
MrJM

Reputation: 1214

This is a different approach from you but I recently fixed this by adding a notification when the service was running

  private void showNotification(){
    NotificationCompat.Builder builer = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Service active")
            .setContentText("Your service keeps running")
            .setOngoing(true);
    mNotificationManager.notify(NOTIFICATION_ID, builer.build());

}

The notification is shown in onStartCommand and dismissed in the service ondestroy method.

Upvotes: 1

Related Questions