我有一个服务,其中的处理程序每5秒必须在logcat中写入“Hello”。但它在logcat上没有任何输出……好像服务没有执行,我在它上面设置了断点,调试模式从未停止在断点上。
我使用以下代码在我的应用程序的第一个活动中启动服务:
startService(new Intent(GPSLoc.this, MyServiceNotifications.class)); //enciendo el service
我确信代码中的startService
被执行了,因为它在启动另一个活动之前被调用,而且其他活动已经开始。
这是我的服务代码:
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");
}
}