简体   繁体   中英

Android: My service doesn't want to start

I have: <service android:name=".kernel.background.Process"></service> Main package is: package="call.com"

I want to create a process which will start sync method for my database, for now I found some example, but..

this is my background process:

package call.com.kernel.background;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class Process extends Service {

private final Timer timer   = new Timer();

@Override
public void onCreate() {
    
    super.onCreate();
    
    startservice();
    
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

private void startservice() {
    
    timer.scheduleAtFixedRate(new TimerTask() {
        
        @Override
        public void run() {
            
            System.out.println("time:");
            System.out.println("is:");
            System.out.println("up:");
            
        }
        
    }, 0, 1000);

    ;
}

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

}

Where are you calling startService(...) from? An Activity somewhere in your code would need to be starting your service. Have you read theAndroid Service Docs yet? They give a lot of insight as to how to properly build and start a service.

If your breakpoint is in the run() method of your TimerTask and it isn't being hit, then it definitely sounds like the service has never been called:

So from your Activity :

Intent i = new Intent(this, Process.class);
startService(i);

I don't think what you are starting is aService . You are starting a timer. You could use a separate Thread to do your update operations. Here is a link to a good article called Painless Threading . Please see the referenced links to learn more about each.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM