我是一名新手学习安卓开发,我有一个使用后台服务的应用程序。 我想通过点击按钮来启动/停止服务,我是这样做的:
case R.id.enablepop:
if (!(pop.runningFlag))
startService(new Intent(mainScreen,PopUpService.class));
return true;
case R.id.disablepop:
if (pop.runningFlag)
stopService(new Intent(mainScreen,PopUpService.class));
return true;
在服务的onStart()函数中,我有一个runningFlag参数,我将其设置为“true”,然后创建一个线程,只要runningFlag为true,线程就会一直运行。在onDestroy()函数中,我将runningFlag设置为false。
问题是服务不会停止。请问有人能帮我解决吗?