Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

android - Why is this simple service not starting?

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");
    }    
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Did you declare the service in AndroidManifest.xml?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...