2

This is my service:

public class KeepAliveService  extends Service {
Alarm alarm = new Alarm();
public void onCreate()
{
    Log.i("","KEEPALIVE onCreate");
    super.onCreate();
    Notification notification = new Notification();
    startForeground(1337, notification);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    alarm.setAlarm(this);
    Log.i("","KEEPALIVE onCreate start command");
    return START_STICKY;
}

@Override
public void onStart(Intent intent, int startId)
{
    alarm.setAlarm(this);
    Log.i("","KEEPALIVE onStart");
}

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

I have this in my manifest:

   <service
        android:name="com.vidyo.vidyomod.KeepAliveService"
        android:exported="true"
        android:process=":KeepAliveService"
        android:enabled="true">
    </service>
    <receiver android:process=":remote" android:name="com.vidyo.vidyomod.utils.Alarm"></receiver>
    <receiver android:name="com.vidyo.vidyomod.utils.AutoStart">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>

And I start it like this:

    Intent i= new Intent(BaseActivity.this, KeepAliveService.class);
    startService(i);

But when I clear my apps , from the app list (The phone square), I get this logs and after that the services doesn't work anymore:

05-16 14:52:22.698: I/ActivityManager(1102): Killing 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false (adj 2): stop com.vidyo.vidyomod
05-16 14:52:22.725: D/ActivityManager(1102): SVC-handleAppDiedLocked: app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}, app.pid = 9049
05-16 14:52:22.726: W/ActivityManager(1102): Scheduling restart of crashed service com.vidyo.vidyomod/.KeepAliveService in 1000ms
05-16 14:52:22.726: V/BroadcastQueue(1102): removeReceiverListLocked app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}
05-16 14:52:22.726: V/BroadcastQueue(1102): removeReceiverListLocked app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}
05-16 14:52:22.726: V/BroadcastQueue(1102): removeReceiverListLocked app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}
05-16 14:52:22.857: I/ActivityManager(1102):   Force stopping service ServiceRecord{3c2a0a1 u0 com.vidyo.vidyomod/.KeepAliveService, isShadow:false}
05-16 14:52:22.859: W/ActivityManager(1102): Spurious death for ProcessRecord{ff18702 0:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}, curProc for 9049: null

EDIT:

Now in my Activity I do this:

    Intent i= new Intent(VidyoModApplication.this, KeepAliveService.class);
    startService(i);

And in my Service, at onStardComand I do this:

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    alarm.setAlarm(this);
    Log.i("","KEEPALIVE onCreate start command");
    Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_HOME);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Notification notification = new Notification.Builder(this)
            .setContentTitle(getText(R.string.app_name))
            .setContentText("Service is running")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pendingIntent)
            .build();

    startForeground(1337, notification);
    return START_STICKY;
}

But still, when I force close the app, it stops the service. Tried it after finding this example: http://www.truiton.com/2014/10/android-foreground-service-example/

rosu alin
  • 5,674
  • 11
  • 69
  • 150

1 Answers1

6

I'm going to go out on a limb here. I don't know the specific devices that you are using. However, there are many devices that have a special settings page for "protected" or "special" apps, that are allowed to run in the background. If you have one of these devices, and your app is NOT in the list of "protected" apps, and your Service is killed, Android will NOT restart it, even if you have declared it as a foreground Service and returned START_STICKY from onStartCommand().

Please check if this is the case on those devices where you are having this problem.

See https://stackoverflow.com/a/41369032/769265 and https://stackoverflow.com/a/42120277/769265

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274