There is a Service:

public class I3 extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { ... return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); sendBroadcast(new Intent("com.mypackage.Restart")); } ... } 

It is launched when the phone is turned on using BroadcastReceiver:

 public class I2 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { context.startService(new Intent(context, I3.class)); } } 

In the manifest, everything is declared correctly:

 ... <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> ... <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="25" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" > <activity android:name=".I1" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".I2" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <action android:name="com.mypackage.Restart" /> </intent-filter> </receiver> <service android:name=".I3" android:label="@string/app_name" /> </application> ... 

In the onStartCommand Service method, onStartCommand starts a new thread ( new MyThread().start(); where is the private class MyThread extends Thread {...} ).

After some time (about 20 hours) the running stream dies for an unknown reason. Then, after some time, the Service start command arrives (the onStartCommand method is onStartCommand ). How can these strange teams be justified? Why does the system kill Service without authorization and start it again after an indefinite period of time? Help solve the problem. Maybe instead of Service, you can use some more reliable component that can work when the phone screen is off?

    1 answer 1

    Why the system arbitrarily kills Service

    This is how the Android OS works - the system can kill any service at its own discretion. For example, due to lack of memory or another reason.

    and starts it again after an indefinite period of time?

    Because you have it START_STICKY , in fact this flag is a request to the system after its "killing" to restart automatically.

    • I understood the problem. What to do? I, in my opinion, made a fairly efficient Service code from memory. Maybe there is a flag strictly prohibiting the "killing" Service? The Play Market works forever, the system never kills its Service. - nick
    • There is no such flag. The only thing I can advise is the automatic start of the service when it is closed. On enSO saw such a solution - google - Barmaley