我怎么能阻止我RECEIVE_BOOT_COMPLETED服务 [英] how can i stop my RECEIVE_BOOT_COMPLETED service
问题描述
看到这个,但即时通讯无法修复该许多问题。
seeing many questions about this but im unable to fix this.
我有这个code
public class myBroadcastReceiver extends BroadcastReceiver {
private final String TAG = "myBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Consts.ANDROID_INTENT_ACTION_BOOT_COMPLEATE)){
Intent newinIntent = new Intent(context, ServiceBootCompleated.class);
context.startService(newinIntent);
}
}
}
它启动一个服务
,我可以用这条线调试它
It starts a Service
and i can debug it using this line
android.os.Debug.waitForDebugger();
我看到返回START_NOT_STICKY;
执行,但仍
该服务是作为一个运行的服务,在可见
Setttings>程序>运行服务
I see that return START_NOT_STICKY;
is executed but still
the service is visible as a "running" service in the
Setttings>programs>Running Services
在的onDestroy()
不会被调用,除非我手动停止。
我有什么做的阻止它,
从Setttings>程序>运行服务窗口中删除了?
the onDestroy()
is never called unless i stop it manually.
What do i have to do to stop it,
remove it from "Setttings>programs>Running Services " window?
推荐答案
一旦你完成你想做的事在后台调用的工作 stopSelf()
Once you have completed the work you wanted to do in the background call stopSelf()
请确保您的服务做任何实质性工作作为后台线程,而不是在的onCreate或onStartCommand。
Be sure that any real work you do in the Service is done as a background thread and not in onCreate or onStartCommand.
请参阅 http://developer.android.com/reference/android /app/Service.html#ServiceLifecycle 有关服务生命周期的更多细节。
See http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle for more details on the Service Lifecycle.
例如:
public int onStartCommand(final Intent intent, final int flags, final int startId)
{
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
//do work
stopSelf();
}
},"MyWorkerThread");
thread.start();
return Service.START_NOT_STICKY;
}
这篇关于我怎么能阻止我RECEIVE_BOOT_COMPLETED服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!