问题描述
所以在我的活动中我有一个计时器, 然后在我的通知中,它显示了具有相同时间的天文钟(我正在为公司构建时间日志应用程序)
现在我要通过计时器的底盘和几个布尔值, 这样我就可以检测到天文钟何时暂停并在通知中显示正确的时间
public class TimerBackgroundService extends Service {
String pausedstring;
long pauSEOffset;
boolean Paused;
boolean Restarted;
long TimeDifference;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
intent.getLongExtra("Offset",pauSEOffset);
intent.getBooleanExtra("Paused",Paused);
intent.getLongExtra("TimeDiffrence",TimeDifference);
intent.getBooleanExtra("Restarted",Restarted);
Intent notificationIntent = new Intent(this,IntTimerActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
RemoteViews collapsedView = new RemoteViews(getPackageName(),R.layout.notificationtimer);
PendingIntent pendingIntent = PendingIntent.getActivity(this,notificationIntent,0);
collapsedView.setimageViewResource(R.id.notifyimage,R.drawable.sp_icon);
pausedstring = "Timer is Running";
collapsedView.setTextViewText(R.id.text_view_collapsed_1,pausedstring);
if(Paused) {
collapsedView.setChronometer(R.id.NotifyChrono,SystemClock.elapsedRealtime(),null,false);
pausedstring = "Timer is Paused";
collapsedView.setTextViewText(R.id.text_view_collapsed_1,pausedstring);
}
if(Restarted){
collapsedView.setChronometer(R.id.NotifyChrono,TimeDifference + SystemClock.elapsedRealtime(),true);
}
Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
// .setContentTitle("SmartApp")
// .setContentText("Timer is Running")
.setSmallIcon(R.drawable.sp_icon)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setShowWhen(true)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
// .setContentTitle("SmartPractice SmartApp")
//
// .setContentText(pausedstring)
.setContent(collapsedView)
// .setCustomContentView(collapsedView)
// .setCustomBigContentView(notificationLayoutExpanded)
.build();
startForeground(1,notification);
//do heavy work on a background thread
//stopSelf();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这是“活动”中我的计时器的代码
chrono.setonChronometerTickListener(new Chronometer.OnChronometerTickListener(){
@Override
public void onChronometerTick(Chronometer chronometer) {
long time = SystemClock.elapsedRealtime() - chronometer.getBase();
h = (int)(time /3600000);
m = (int)(time - h*3600000)/60000;
int s= (int)(time - h*3600000- m*60000)/1000 ;
String t = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
chronometer.setText(t);
}
});
chrono.setBase(SystemClock.elapsedRealtime());
chrono.setText("00:00:00");
Intent serviceIntent = new Intent(this,TimerBackgroundService.class);
notificationmanager = notificationmanagerCompat.from(this);
start.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startTimerService();
chrono.setBase(SystemClock.elapsedRealtime() - pauSEOffset);
chrono.start();
}
});
pause.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pauSEOffset=SystemClock.elapsedRealtime() - chrono.getBase();
pauseTimerService();
chrono.stop();
}
});
stop.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopTimerService();
chrono.stop();
String tyd =(String) chrono.getText();
pauSEOffset = 0;
String[] split = tyd.split(":");
String Hours = split[0];
String Minutes = split[1];
String Seconds = split[2];
hoursTextView.setText(Hours);
int sec = Integer.parseInt(Seconds);
int min = Integer.parseInt(Minutes);
int one = 1;
int sum = min + one;
if(sec >= 30) {
MinutesText.setText(String.valueOf(sum));
}
else {
MinutesText.setText(Minutes);
}
chrono.setBase(SystemClock.elapsedRealtime());
}
});
}
public void startTimerService(){
Intent serviceIntent = new Intent(this,TimerBackgroundService.class);
ContextCompat.startForegroundService(this,serviceIntent);
if(Paused){
boolean Restarted = true;
serviceIntent.putExtra("Restarted",Restarted);
}
}
public void pauseTimerService(){
Intent serviceIntent = new Intent(this,TimerBackgroundService.class);
long timeDifference = 0;
Paused = true;
timeDifference = chrono.getBase() - SystemClock.elapsedRealtime();
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
serviceIntent.putExtra("Offset",pauSEOffset);
serviceIntent.putExtra("TimeDiffrence",timeDifference);
serviceIntent.putExtra("Paused",Paused);
ContextCompat.startForegroundService(this,serviceIntent);
}
public void stopTimerService(){
Intent serviceIntent = new Intent(this,TimerBackgroundService.class);
stopService(serviceIntent);
}
然后为您提供方便,这是我传递意图的地方
public void pauseTimerService(){
Intent serviceIntent = new Intent(this,TimerBackgroundService.class);
long timeDifference = 0;
Paused = true;
timeDifference = chrono.getBase() - SystemClock.elapsedRealtime();
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
serviceIntent.putExtra("Offset",pauSEOffset);
serviceIntent.putExtra("TimeDiffrence",timeDifference);
serviceIntent.putExtra("Paused",Paused);
ContextCompat.startForegroundService(this,serviceIntent);
}
以及我的意图
public int onStartCommand(Intent intent,int startId) {
intent.getLongExtra("Offset",pauSEOffset);
intent.getBooleanExtra("Paused",Paused);
intent.getLongExtra("TimeDiffrence",TimeDifference);
intent.getBooleanExtra("Restarted",Restarted);
Intent notificationIntent = new Intent(this,IntTimerActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
RemoteViews collapsedView = new RemoteViews(getPackageName(),R.layout.notificationtimer);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0);
collapsedView.setimageViewResource(R.id.notifyimage,R.drawable.sp_icon);
pausedstring = "Timer is Running";
collapsedView.setTextViewText(R.id.text_view_collapsed_1,pausedstring);
if(Paused) {
collapsedView.setChronometer(R.id.NotifyChrono,false);
pausedstring = "Timer is Paused";
collapsedView.setTextViewText(R.id.text_view_collapsed_1,pausedstring);
}
if(Restarted){
collapsedView.setChronometer(R.id.NotifyChrono,true);
}
至于我已经尝试过的,就是检查我的意图键是否正确,并在意图addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
中添加标志
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)