1

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

我做了什么

我的活动的一部分:

    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);
    }

}

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

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

4

1 回答 1

1

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

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

它工作正常。

于 2013-07-31T21:37:33.517 回答