停止或中止服务 [英] Stop or kill a service
问题描述
我开发的任务管理器应用程序。
I am developing a task manager application..
在该应用程序,我向用户显示正在运行的应用列表,并杀死按钮每个应用程序的旁边。 当用户presses杀按钮相应的活动将被杀死,我用
In that app, I display to the user a list of running apps and kill button beside each app. When user presses kill button the corresponding activity will be killed and i used
activitymanager.killbackgroundprocesses(packageName)
我创建了一个叫做服务通知另一个应用程序,其中用户可以启动,并通过点击按钮,分别为启动服务按钮,一站式服务停止服务。然后运行我的任务管理器应用程序之前,我开始从服务通知服务应用程序。 因此,这项活动被显示在我的任务管理器应用程序。
I created another application called service notification where user can start and stop a service by clicking on button "start service" and button "stop service" respectively.. Then before running my task manager app, i started the service from service notification app. So this activity was displayed in my task manager app.
但是,当我杀了那个活动,它从列表中消失,然后再次出现,因为服务是restarting..How我杀了服务队呢?
But when i kill that activity, it disappears from the list and then appears again because the service is restarting..How do I kill the sevice then?
我用startService和的onCreate启动服务,并呈现出一些通知和stopService和的onDestroy停止服务......
I used startService and onCreate for starting service and showing some notification and stopService and onDestroy for stopping service...
推荐答案
我终于找到了答案,我的问题...
I finally found an answer to my question...
使用后
activitymanager.killbackgroundprocesses(packageName) ,
我们要检查是否有根据该软件包的名称或进程名运行的任何服务。
we have to check if there are any services running under that package name or process name.
然后,我们不得不使用
stopService(new Intent().setComponent(serviceInfos.get(i).service))
但它会返回一个错误,因为Android不容许我们停止另一个服务
But it would return an error because android does not allow us to stop another service
所以,我用清单这两行,然后跑到我的构建机器的应用程序
So I used these two lines in the manifest and then ran my app in the build machine
coreApp="true"
android:sharedUserId="android.uid.system"
这两条线有没有在Android设置清单文件,它允许设置,如果用户想要停止服务。
These two lines are there in the android settings manifest file, which allows settings to stop service if the user wants.
所以,该活动得到了与该服务杀害沿... :)
So the activity got killed along with the service... :)
和只记得这个工作在构建机上......
And remember this worked only on the build machine....
这篇关于停止或中止服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!