Android custom notification by remoteViews

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43219615/article/details/100046209

1 Introduction

We can customize a push notification by remoteViews, but remoteViews there are many restrictions. remoteViews widget only supports the following: AnalogClock, Button, Chronometer, ImageButton , ImageView, ProgressBar, TextClock, TextView; supports only the following layout: AdapterViewFlipper, the FrameLayout, GridLayout, the GridView
, the LinearLayout, the ListView, the RelativeLayout, StackView
, ViewFlipper.
image
image
Main methods:
setViewVisibility: setting specifies whether the control is visible.
setViewPadding: setting the pitch of the specified control.
setTextViewText: setting specifies TextView or Button control's text.
setTextViewTextSize: setting specifies TextView or Button control text size.
setTextColor: Set the text color specified TextView or Button control.
setTextViewCompoundDrawables: setting specifies TextView or Button control of the surrounding text icon.
setImageViewResource: Set ImageView or ImgaeButton control resource number.
setImageViewBitmap: Set the bitmap object ImageView or ImgaeButton control.
setChronometer: Set the timer information.
setProgressBar: progress bar provided information, including the maximum value and the current progress.
setOnClickPendingIntent: Click Settings specified control response action.
Notification to call the object after the completion of construction and setting RemoteViews object setContent methods, to complete custom notification definition.

2. Example

  1. Custom layout file notifications.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="60dp"
    >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="fitCenter"
        android:src="@drawable/tt"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:orientation="vertical"
        >

        <ProgressBar
            android:id="@+id/pb_play"
            android:layout_width="match_parent"
            style="?android:progressBarStyleHorizontal"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:max="100"
            android:progress="10"/>

        <TextView
            android:id="@+id/tv_play"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:textSize="17sp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <Chronometer
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/ch_play"/>

        <Button
            android:id="@+id/btn_play"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:text="暂停"
            android:textColor="@color/black"
            android:textSize="17sp"/>
    </LinearLayout>
</LinearLayout>
  1. Use custom notification code.
private void sendCustomNotification(Context context, String song, boolean isPlay, int progress, long time) {
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.remote_view_demo);

        if(isPlay) {
            remoteViews.setTextViewText(R.id.tv_play, "正在播放" + song);
            remoteViews.setTextViewText(R.id.btn_play, "暂停");
            remoteViews.setChronometer(R.id.ch_play, time, "%s", true);
        } else {
            remoteViews.setTextViewText(R.id.tv_play, "暂停播放" + song);
            remoteViews.setTextViewText(R.id.btn_play, "播放");
            remoteViews.setChronometer(R.id.ch_play, time, "%s", false);
        }

        remoteViews.setProgressBar(R.id.pb_play, 100, progress, false);
        remoteViews.setOnClickPendingIntent(R.id.btn_play, pendingIntent);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "message");
        builder.setContentIntent(pendingIntent).setContent(remoteViews).setTicker(song).setSmallIcon(R.drawable.tt_s);
        Notification notification = builder.build();
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);
    }

Guess you like

Origin blog.csdn.net/weixin_43219615/article/details/100046209