问题描述
我有一个处理程序,必须在每5秒logcat中写你好的服务。但它并没有写任何的logcat的......这就像该服务不执行,我把一个断点,并在调试模式永远不会停止在断点处。
我启动该服务,在我的应用程序的第一个活动,这一点:
startService(新意图(GPSLoc.this,MyServiceNotifications.class)); // enciendo埃尔服务
我相信,在code startService
被执行,因为它开始另一个活动之前调用,其他活动开始。
这是我服务的code:
公共类MyServiceNotifications延伸服务{
布尔serviceStopped;
私人处理器mHandler;
私人可运行updateRunnable =新的Runnable(){
@覆盖
公共无效的run(){
如果(serviceStopped ==假)
{
createNotificationIcon();
}
queueRunnable();
}
};
私人无效queueRunnable(){
// 600000:CADA 10 minutos,comprueba SI干草NUEVAS notificacionesÿactualiza拉
//通知栏
mHandler.postDelayed(updateRunnable,5000);
}
@覆盖
公众的IBinder onBind(意向意图){
返回null;
}
@覆盖
公共无效的onCreate(){
serviceStopped = FALSE;
// ////////////////////////////////////// MANEJADOR类似于联合国HILO
mHandler =新的处理程序();
queueRunnable();
// ///////////////////////////////////// FIN MANEJADOR
}
@覆盖
公共无效的onDestroy(){
serviceStopped = TRUE;
}
@覆盖
公共无效ONSTART(意向意图,诠释startid){
}
公共无效createNotificationIcon()
{
Log.d(MyServiceNotifications,你好);
}
}
你声明的的AndroidManifest.xml
?
I have a service with a handler that has to write "Hello" in the logcat every 5 seconds. But it doesn't write nothing on the logcat... It's like the service is not executing, and I put a breakpoint on it and the debug mode never stops on the breakpoint.
I start the service, in the first activity of my app, with this:
startService(new Intent(GPSLoc.this, MyServiceNotifications.class)); //enciendo el service
I am sure that the code startService
is executed because it is called before starting another activity, and the other activity starts.
This is the code of my service:
public class MyServiceNotifications extends Service {
boolean serviceStopped;
private Handler mHandler;
private Runnable updateRunnable = new Runnable() {
@Override
public void run() {
if (serviceStopped == false)
{
createNotificationIcon();
}
queueRunnable();
}
};
private void queueRunnable() {
// 600000 : cada 10 minutos, comprueba si hay nuevas notificaciones y actualiza la
// notification BAR
mHandler.postDelayed(updateRunnable, 5000);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
serviceStopped = false;
// //////////////////////////////////////MANEJADOR SIMILAR A UN HILO
mHandler = new Handler();
queueRunnable();
// ///////////////////////////////////// FIN MANEJADOR
}
@Override
public void onDestroy() {
serviceStopped = true;
}
@Override
public void onStart(Intent intent, int startid) {
}
public void createNotificationIcon()
{
Log.d("MyServiceNotifications", "Hello");
}
}
Did you declare the service in AndroidManifest.xml
?
这篇关于为什么这个简单的服务没有启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!