首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >服务和时钟同步

服务和时钟同步
EN

Stack Overflow用户
提问于 2013-07-31 20:23:48
回答 1查看 1.6K关注 0票数 1

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

我所做的

我活动的一部分是:

代码语言:javascript
复制
    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;
           }

       }
   }

作为我服务的一部分:

代码语言:javascript
复制
    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);
    }

}

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

正如您所看到的,我还发送了另一个变量,活动正确地读取了它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-31 21:37:33

我已经找到了解决我的问题的办法。这很简单。它需要使用标志(PendingIntent.FLAG_UPDATE_CURRENT)

代码语言:javascript
复制
PendingIntent contentintent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

工作也很好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17980245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档