Android: Re-invoke application if task manager kill

前端 未结 5 2148
谎友^
谎友^ 2021-02-09 00:09

Application thread get close if its killed by task manager. Need to re-invoke application as though its killed by other application or task manager. Any idea?

5条回答
  •  伪装坚强ぢ
    2021-02-09 01:05

    Bug in Android 2.3 with START_STICKY

    I needed to keep alive a Service with all my forces. If the service is running anytime you can pop the UI.

    onDestroy()
    

    it will re-launch.

    Can't be uninstalled the app, because it has a Device Administrator.

    It is a kind of parental control, the user knows it is there. Only way to stop is to remove the Device Admin, and uninstall it, but removing Device Admin will lock the phone as Kaspersky how it does.

    There are a loot of braodcast receivers, such as boot finshed, user presen, screen on, screen off... , many other, all starting the service, you can do it with UI too. Or in the service check if your activity alive , visible, if not, than pop it.

    I hope you will use with good reason the info!

    Edit: Restart service code snippet:

        // restart service:
        Context context = getApplicationContext();
        Intent myService = new Intent(context, MyService.class);
        context.startService(myService);
    

    Edit2: add spippet to check if the service is running in ... a load of Broadcasts

       public static  boolean isMyServiceRunning(Context context) {
            ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
                if (MyService.class.getName().equals(service.service.getClassName())) {
                    Log.d("myTag", "true");
                    return true;
                }
            }
            Log.d("myTag", "false");
            return false;
        }
    

    Edit3 other service start:

       public static void startTheService(Context context) {
             Intent myService = new Intent(context, MyService.class);
             context.startService(myService);
       }
    

    Dont't forget Android 2.3 bug: do the logic for initialization in

    @Override
    public void onCreate()
    

    and not in:

        @Override
        public int onStartCommand(Intent intent, int flags, int startId)
    

提交回复
热议问题