Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Why doesn't my Service work in Android? (I just want to log something ever 5 seconds)

I created a new class called HelloService. I added this to the Android manifest.xml.

public class HelloService extends Service {
    private Timer timer = new Timer();
    private long INTERVAL = 5000;

    public void onCreate() {
        super.onCreate();
        startservice();

    }

    private void startservice() {
        timer.scheduleAtFixedRate( new TimerTask() {
            public void run() {
                Log.d("servy", "This proves that my service works.");
            }
        }, 0, INTERVAL);
    ; }

    private void stopservice() {
        if (timer != null){
            timer.cancel();
        }
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

My other activity calls it like this:

    Intent helloservice = new Intent(this, HelloService.class);
    startService(helloservice);

For some reason, I put a breakpoint in my new HelloService...but it's not even hitting. It's not logging either.

Edit: "Unable to start service Intent { cmp = com.examples.hello/.HelloService }: not found"

What does that mean? ... I created HelloService.java in the same place as everything else...


Solved. I fixed my manifest file. Thanks Nikola Smiljanic

<service android:name=".HelloService"/>

to:

   <service android:name="HelloService"></service>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Maybe you're not declaring the service in your manifiest. Anyway you're using the wrong class. You have to use AlarmManager for programing events. See that link, it was very useful to me.

Use alarmManager and service to perform schedule notification only during specific time period


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...