How to automatically restart a service even if user force close it?

后端 未结 10 765
感情败类
感情败类 2020-11-27 11:19

I want a service to run all the time in my application. So I want to restart it even if it is force closed by user. There is definitely a way to do it as apps like facebook

相关标签:
10条回答
  • 2020-11-27 11:56

    on the service's startCommand method return START_STICKY. generally it tell the OS to start the service when it is killed.

    0 讨论(0)
  • 2020-11-27 11:56

    If the situation allows to use 'root' it's usually possible to implement Humpty-Dumpty paradigm.

    Your application (1st) installs another application (2nd, taking APK from assets) and runs the service of the 2nd app. 2nd app's service bind to the 1st app service and rebinds when disconnected. The 1st app does the same.

    Sure it will not help when all apps are killed by some Free RAM or similar application but when Android kills either of those two, the other one will restart its counterpart.

    0 讨论(0)
  • 2020-11-27 11:58

    You have to create a sticky service with overriding onTaskRemoved method, where you can set an alarm service to trigger your code again.

    public class BackgroundService extends Service {
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return START_STICKY;
        }
    
        @Override
        public void onTaskRemoved(Intent rootIntent) {
            //create an intent that you want to start again.
            Intent intent = new Intent(getApplicationContext(), BackgroundService.class);
            PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 5000, pendingIntent);
            super.onTaskRemoved(rootIntent);
        }
    }
    

    Also in some devices like Xiaomi, Huwaei the app gets force closed once it's removed from recent apps. This is because the manufacturers have task manager features which improve ram/battery performance.

    You can check this link for more information: https://stackoverflow.com/a/41360159/2798289

    0 讨论(0)
  • 2020-11-27 12:03

    Whenever a service is killed, its onDestroy method is always called. Its better to use a BroadcastReceiver to start your service when it is killed.

    Here is a sample code illustrating its implementation:-

    @Override
    public void onDestroy() {
    Intent in = new Intent();
    in.setAction("StartkilledService");
    sendBroadcast(in);
    Log.d("debug", "Service Killed");
    }
    

    Then register a receiver in AndroidManifest.xml:-

    <receiver android:name=".app.ServiceDestroyReceiver" >
        <intent-filter>
            <action android:name="StartKilledService" >
            </action>
        </intent-filter>
    </receiver>
    

    Finally,create a BroadcastReceiver,and start your service in the onReceive method:-

    @Override
    public void onReceive(Context context, Intent intent) {
    Log.d("debug", "ServeiceDestroy onReceive...");
    Log.d("debug", "action:" + intent.getAction());
    Log.d("debug", "Starting Service");
    ServiceManager.startService();
    }
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题