【发布时间】:2017-04-05 17:32:57
【问题描述】:
我正在使用计数器 (0 - 10) 之类的服务来测试服务,但是当应用程序被终止时,服务已经重新启动而不是继续。
在logcat中显示:
04-05 17:25:47.497 25309-25309/? I/livro: onCreate
04-05 17:25:47.498 25309-25309/? I/livro: onStartCommand
04-05 17:25:48.531 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...0
04-05 17:25:49.571 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...1
04-05 17:25:50.611 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...2
04-05 17:25:51.651 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...3
04-05 17:25:52.692 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...4
04-05 17:25:53.733 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...5
04-05 17:25:54.771 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...6
04-05 17:25:55.811 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...7
04-05 17:25:56.851 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...8
04-05 17:25:57.891 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...9
04-05 17:25:57.891 25309-25309/com.example.augustoc.helloservice I/livro: onDestroy
当关闭应用程序时,计数器已重新启动,但我希望该服务继续。
在 onStartCommand 中:
@Override // ID DESTE SERVIÇO
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,"onStartCommand");
count = 0;
running = true;
//delega para a trhead
new WorkerThread().start();
return START_STICKY;
}
我尝试返回 START_STICKY 和 START_NOT_STICKY,两者都不起作用。
【问题讨论】:
-
onStartCommand 正在从某个 onCreate() 方法执行?
-
“应用程序被杀死”是什么意思。你是如何“杀死应用程序”的?当您关闭应用程序时,
Service是被 Android 杀死并自动重新启动,还是Service只有在您再次运行应用程序时才重新启动?你在什么设备上测试? -
“应用程序被杀死”是当我关闭应用程序时。当我关闭应用程序时,服务会自动重新启动,但我希望服务继续而不是重新启动。
-
“关闭应用程序”是什么意思。 Android 应用程序没有“关闭”。我了解您想要什么(
Service不会停止),但我想知道您如何知道Service已停止!请回答我的问题。另外,您在什么设备和什么版本的 Android 上进行测试? -
@DavidK
onStartCommand()在Service启动时从 Android 框架调用。它不是从onCreate()调用的。