使用service定期执行一个服务

import android.app.Service;
import android.os.Handler;

public class PeriodicService extends Service {

private Handler mPeriodicEventHandler;

    private final int PERIODIC_EVENT_TIMEOUT = 30000;

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

      mPeriodicEventHandler = new Handler();
      mPeriodicEventHandler.postDelayed(doPeriodicTask, PERIODIC_EVENT_TIMEOUT);
    }

    private Runnable doPeriodicTask = new Runnable()
    {
        public void run() 
        {
            //your action here
            mPeriodicEventHandler.postDelayed(doPeriodicTask, PERIODIC_EVENT_TIMEOUT);
        }
    };

    @Override
    public void onDestroy() {

        mPeriodicEventHandler.removeCallbacks(doPeriodicTask);      
        super.onDestroy();
    }
}

你可能感兴趣的:(android,OS)