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?