How to create a Chronometer in a notification?

0

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 launches a notification in the status bar, as shown below in the first notification:

To close 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 do I create a Chronometer in a notification?

    
asked by anonymous 12.06.2017 / 23:53

1 answer

1

As asked by @wakim, I did a brief search on the RemoteViews class in which you can use the setChronometer () to create a custom notification. Here's how it should look:

JAVA

 RemoteViews remoteWidget = new RemoteViews(getPackageName(), R.layout.custom_notification);
 remoteWidget.setChronometer(R.id.noteChronometer, value, null, true);

 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

 NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentText("")
            .setContentTitle("titulo")
            .setSmallIcon(R.drawable.ic_today_black_24dp)
            .setAutoCancel(false)
            .setOnlyAlertOnce(true)
            .setContentIntent(
                    PendingIntent.getActivity(context, 10,
                            new Intent(context, MapsActivity.class)
                                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
                            0)
            );

 /* define customização da notificação*/
 builder.setCustomContentView(remoteWidget);

 notificationManager.notify(0, builder.build());

XML: custom_notification.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="4dp">
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="Stop"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:layout_alignBottom="@+id/btn"
            android:layout_toRightOf="@+id/noteImageView"
            android:layout_toEndOf="@+id/noteImageView"
            android:layout_marginBottom="10dp">

                <TextView
                    android:id="@+id/note_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="4dp"
                    android:textSize="14sp"
                    android:textColor="#555555"
                    android:text="Tempo: " />

                <Chronometer
                    android:id="@+id/noteChronometer"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:textColor="#222222"/>

        </LinearLayout>

        <ImageView
            android:id="@+id/noteImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_local_shipping_black_48dp"
            android:layout_alignBottom="@+id/btn"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
</RelativeLayout>

Result:

    
13.06.2017 / 23:05