我有以下方法:
public NotificationCompat.Builder createNotification(Context context) {
Intent intent = new Intent(this, MapsActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
boolean running = true;
builder = new NotificationCompat.Builder(context)
.setContentText("conteúdo")
.setContentTitle("titulo")
.setSmallIcon(R.drawable.ic_today_black_24dp)
.setAutoCancel(false)
.setOnlyAlertOnce(true)
.setOngoing(running)
.setContentIntent(
PendingIntent.getActivity(context, 10,
new Intent(context, MapsActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
0)
)
.addAction(running ? R.drawable.ic_stop_black_24dp
: R.drawable.ic_play_arrow_black_24dp,
running ? "Pause"
: "play",
pIntent)
.addAction(R.drawable.ic_stop_black_24dp, "Stop",
pIntent);
notificationManager.notify(0, builder.build());
return builder;
}
其中在状态栏中启动了通知,如下面第一个通知中所示:
要链接通知,请执行以下操作:
NotificationCompat.Builder notification = createNotification(this);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notification.build());
我想在通知中创建一个计时器,就像它在Strava通知中所显示的那样(图像的第二个通知),如上所示。
如何在通知中创建Chronometer
?
答案 0 :(得分:2)
因此,如果您知道如何使用Layouts
,那么创建App Widget布局很简单。但是,您必须知道App Widget布局基于RemoteViews
,它不支持所有类型的布局或视图窗口小部件。无论如何,如果你需要与App Widget布局一起使用,这里有一些指导:
https://developer.android.com/guide/practices/ui_guidelines/widget_design.html
幸运的是,您可以从开发人员网站上看到RemoteViews
支持Chronometer
:https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
认为你可以像往常一样访问你的Chronometer
并且你可以做这样的事情取决于你希望它暂停,恢复,等等:
remoteView.setChronometer(R.id.myChronometere, SystemClock.elapsedRealtime(),
null, false); //pausing
答案 1 :(得分:1)
您可以使用内置的计时器:
builder = new NotificationCompat.Builder(context)
builder.setUsesChronometer(true)
...
builder.build()