Android 服务与天文钟同步
我需要能够启动计时器,然后关闭活动,之后通过通知,返回到该活动,并在计时器中看到正确的时间 我所做的 我活动的一部分:Android 服务与天文钟同步,android,android-intent,android-service,chronometer,Android,Android Intent,Android Service,Chronometer,我需要能够启动计时器,然后关闭活动,之后通过通知,返回到该活动,并在计时器中看到正确的时间 我所做的 我活动的一部分: public void doClick(View target) { switch(target.getId()) { case R.id.buttonStart: { Mchronometer.setBase(SystemClock.elapsedRealti
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);
}
}
我试图从活动向服务发送一条消息,其中包含已用信息。
如果我先在我的设备上启动它(系统加载后),它工作正常,但当我再次启动它时。活动收到错误消息。它接收设备上启动的第一个服务的时间
正如您所见,我还发送了一个变量,activity正确地读取了它。我找到了问题的解决方案。 很简单。 需要使用flag(pendingent.flag\u UPDATE\u CURRENT)
而且工作很好 我找到了问题的解决办法。 很简单。 需要使用flag(pendingent.flag\u UPDATE\u CURRENT) 而且工作很好
PendingIntent contentintent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);