防止停止Android服务

问题描述 投票:1回答:2

我有一个使用以下代码从Activity调用的服务:

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

服务运行良好大约20-30分钟,但在该服务停止运行后,我知道我可以使用'前台'服务,但通过使用我应该显示通知,所以,还有其他方法来阻止服务停止运行?

java android service android-activity
2个回答
0
投票

我不确定,但我认为你是从UI线程开始你的服务,然后你将你的应用程序背景放在一边,所以一段时间后你的活动实例迷失了,那时UI线程也被杀死了。

创建一个新线程并使用该线程而不是UI线程启动服务,因为服务正在线程正在调用它的线程上工作。

//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();

它可能对你有所帮助。


0
投票

如果您在设备上使用TaskManager应用程序,则应注意不要破坏您的应用程序和/或服务。

© www.soinside.com 2019 - 2024. All rights reserved.