服务和计时器同步

时间:2013-07-31 20:23:48

标签: android android-intent android-service chronometer

我需要能够启动计时器,然后关闭活动,之后通过通知,返回到该活动,并在计时器中查看正确的时间。

我做了什么

我的活动的一部分:

    public void doClick(View target)
   {
       switch(target.getId())
       {
           case R.id.buttonStart:
           {

               Mchronometer.setBase(SystemClock.elapsedRealtime());
               Mchronometer.start();
               Intent intent = new Intent(RecentActivity.this, ChronometerService.class);
               intent.putExtra("task_name",task_name);
               intent.putExtra("task_id",task_id);
               intent.putExtra("ellapsedTime",Mchronometer.getBase());
               Log.d("base",""+Mchronometer.getBase());
               startService(intent);
               break;
           }
           case R.id.buttonStop:
           {
               stopService(new Intent(RecentActivity.this, ChronometerService.class));
               Mchronometer.stop();
               Mchronometer.setBase(SystemClock.elapsedRealtime());
               break;

           }
           case R.id.button3:
           {

               break;
           }

       }
   }

我服务的一部分:

    public class ChronometerService extends Service {

    private  ThreadGroup myThreads = new ThreadGroup("ServiceWorker");
    private NotificationManager notificationMgr;
    private int task_id;
    private long ellapsedTime;
    @Override
    public void onCreate() {
        super.onCreate();
        notificationMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        String task_name =intent.getExtras().getString("task_name");
        task_id =intent.getExtras().getInt("task_id");
        ellapsedTime = intent.getExtras().getLong("ellapsedTime");
        Log.d("servicebase",""+ellapsedTime);
        displayNotificationMessage(task_name);
        new Thread(myThreads, new ServiceWorker(),"ChronometerService").start();
        return START_STICKY;
    }
    private class ServiceWorker implements Runnable {

        public void run() {

        }

    }

    @Override
    public void onDestroy()
    {
        myThreads.interrupt();
        notificationMgr.cancelAll();
        super.onDestroy();
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    public void displayNotificationMessage(String message){
        Notification notification = new Notification(R.drawable.emo_im_winking,message,System.currentTimeMillis());
        notification.flags = Notification.FLAG_NO_CLEAR;
        Intent intent = new Intent(this, RecentActivity.class);
        intent.putExtra("task_id", task_id);
        intent.putExtra("ellapsedTime",ellapsedTime);
        Log.d("servicebase1",""+Long.toString(ellapsedTime));
        PendingIntent contentintent = PendingIntent.getActivity(this,0,intent,0);
        notification.setLatestEventInfo(this,"ChronometerService",message,contentintent);
        notificationMgr.notify(0, notification);
    }

}

我尝试将活动中的消息发送到服务,其中包含已过去的信息。 如果我首先在我的设备上启动它(在系统加载后),它可以正常工作,但是当我再次启动它时。活动收到错误的消息。它接收在设备上启动的第一个服务的时间。

如您所见,我还发送了一个变量,并且活动正确读取它。

1 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法。 这很简单。 需要使用flag(PendingIntent.FLAG_UPDATE_CURRENT)

PendingIntent contentintent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

它的工作正常。