如何在通知Android中创建计时器? [英] How to create a chronometer in a notification Android?

查看:49
本文介绍了如何在通知Android中创建计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有以下方法:

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;
}

其中在状态栏中启动了通知,如下第一个通知所示:

In which launched a notification in the status bar, as shown below in the first notification:

要链接通知,我需要这样做:

To link the notification I do this:

NotificationCompat.Builder notification = createNotification(this);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notification.build());

我想在通知中创建一个计时器,就像在Strava通知(图像的第二个通知)中显示的一样,如上所示.

I would like to create a Chronometer in a notification, exactly as it appears in the Strava notification (second notification of the image), as shown above.

如何在通知中创建天文钟?

推荐答案

因此,如果您知道如何使用 Layouts ,则创建App Widget布局很简单.但是,您必须注意,App Widget布局基于 RemoteViews ,它不支持每种布局或视图Widget.无论如何,如果您需要与App Widget布局相关的帮助,请参考以下指南: https://developer.android.com/guide/practices/ui_guidelines/widget_design.html

So creating the App Widget layout is simple if you know how to work with Layouts. However, you have to be aware that App Widget layouts are based on RemoteViews, which do not support every kind of layout or view widget. Anyway if you need asistance with App Widget layout here is some guaidance: https://developer.android.com/guide/practices/ui_guidelines/widget_design.html

很高兴为您提供 RemoteViews 支持 Chronometer ,您可以从开发人员网站上看到:

Luckily for you RemoteViews support Chronometer as you can see from developer webiste: https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout

您认为您可以像往常一样访问天文钟,并且您可以执行以下操作,具体取决于您要暂停,恢复还是执行以下操作:

A think you can acces your Chronometer as usually and you could do something like this depends do you want it to pause, resume, whatever:

remoteView.setChronometer(R.id.myChronometere, SystemClock.elapsedRealtime(),
                      null, false); //pausing

这篇关于如何在通知Android中创建计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆