注意:service要enable,不然intent不了。
在service中使用timer:
public class MyService extends Service { public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public void onCreate() { startTimer(); super.onCreate(); } @Override public void onDestroy() { stopTimer(); super.onDestroy(); } public void startTimer(){ if (timer==null){ timer=new Timer(); timerTask=new TimerTask() { @Override public void run() { System.out.println("service----"); } }; timer.schedule(timerTask,1000,1000); } } public void stopTimer(){ if (timer!=null){ timerTask.cancel(); timer.cancel(); timerTask=null; timer=null; } } private java.util.Timer timer=null; private TimerTask timerTask=null; }