viana
viana

Reputation: 515

How to create a chronometer in a notification Android?

I have the following method below:

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:

enter image description here

To link the notification I do this:

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

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.

How to create a Chronometer in a notification?

Upvotes: 0

Views: 5237

Answers (2)

Yazon2006
Yazon2006

Reputation: 3752

You can use built in chronometer:

builder = new NotificationCompat.Builder(context)
builder.setUsesChronometer(true)
...
builder.build()

Upvotes: 3

Yupi
Yupi

Reputation: 4470

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

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

Upvotes: 8

Related Questions