Time taken for Android Service to restart after being forcibly killed

后端 未结 2 622
南方客
南方客 2020-12-20 02:28

When I use an app such as \"Go Power Master\" to forcibly kill Services running on my Android phone, not all Services restart with the same delay. Why is this and how do I r

相关标签:
2条回答
  • 2020-12-20 02:57

    The solution to this problem seems to be the following. Cause the OS to believe that it has killed a Service that was doing work. It then appears to restart that Service with a smaller delay.

    I have now edited my Service to start an AsyncTask which simply loops forever and calls Thread.sleep(100)

    Now when I forcibly kill my Service, I see very similar restart times that I do for the Facebook Service: 5000ms, 24713ms, 14997ms, 54912ms, 14988ms.

    Please prove me wrong or right but as it stands this seems to solve my problem.

    0 讨论(0)
  • 2020-12-20 03:09

    I believe it greatly depends on the importance of the process that was killed. When you use a task killer to kill an application, the Android OS kills it the same way it would if the system was running low on memory. Since it is killed in the same way, applications or services will be restarted by the Android OS based on the "importance" of the application.

    To make a service more important (thus being restarted quicker) you could add a notification to the service. This notification would be visible in the notification bar. Since the service was visible to the user, it will be restarted quicker than applications or other services that were not visible.

    Also, a service that is bounded to an Activity will have greater importance than an unbounded service.

    For more information check out this http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html (The process life cycle portion)

    EDIT Get importance of all running processes.

    List<RunningAppProcessInfo> procInfo = activityManager.getRunningAppProcesses();
    for(int i = 0; i < procInfo.size(); i++){
        int importance = procInfo.get(i).importance;
        //Print importance and Package name to log
    }
    
    0 讨论(0)
提交回复
热议问题