简体   繁体   中英

Prevent stopping android service

I've a Service that is called from an Activity with this code:

startService(new Intent(AMC_Activity.this,CallService.class));

Service is running good for about 20-30 minutes, but after that service stop running, I know that I can use 'foreground' service, but by using that I should show a notification, so, is there any other way to prevent service stop running?

I am not sure but i think you are starting your service from UI thread and after that you are putting your in application background so after sometime your activity instance getting lost and that UI thread also killed at that time.

Solution

Create a new Thread and start service with that Thread instead of UI Thread, because service is doing work on that Thread whoever Thread is invoking it.

//This code will be in class body.
private Thread thread1 = new Thread(){
    public void run(){
        startService(new Intent(AMC_Activity.this,CallService.class));
    }
}

//Now this code will be call when you are going to start the service, i.e. Under onCreate()
thread1.start();

It may helpful to you.

如果您在设备上使用TaskManager应用程序,则应注意它也不会杀死您的应用程序和/或服务。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM