我正在使用以下代码在前台服务通知上使用NotificationCompat.Builder setUsesChronometer创建一个计时器:
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("data","somedata");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(context, PRIMARY_CHANNEL)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message
))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.app_icon_white);
mBuilder.setColor(context.getResources().getColor(R.color.theme_color));
} else {
mBuilder.setSmallIcon(R.drawable.app_icon_white);
}
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.outgoing);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getResources().getString(R.string.feroz_channel_name);
String description = context.getResources().getString(R.string.feroz_channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL, name, importance);
channel.enableLights(true);
channel.setLightColor(context.getResources().getColor(R.color.theme_color));
channel.enableVibration(true);
channel.setDescription(description);
channel.setSound(soundUri, new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());
NotificationManager notificationManager1 = context.getSystemService(NotificationManager.class);
notificationManager1.createNotificationChannel(channel);
}
mBuilder.setUsesChronometer(true);
mBuilder.setContentIntent(pendingIntent);
我在此通知上设置了待处理的意图。我如何通过/使用计时器,当用户单击通知并打开MyActivity时,它应该显示相同的计时器?请提出从同步到活动保持同步/共享计时码表的方法是什么?