Android通过remoteViews自定义通知

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43219615/article/details/100046209

1.简介

通过remoteViews我们可以自定义通知推送,但是remoteViews也有许多限制。remoteViews仅支持以下小部件:AnalogClock、Button、Chronometer、ImageButton、ImageView、ProgressBar、TextClock、TextView;仅支持以下布局:AdapterViewFlipper、FrameLayout、GridLayout、GridView
、LinearLayout、ListView、RelativeLayout、StackView
、ViewFlipper。
图片
图片
主要方法:
setViewVisibility:设置指定控件是否可见。
setViewPadding:设置指定控件的间距。
setTextViewText:设置指定TextView或Button控件的文字内容。
setTextViewTextSize:设置指定TextView或Button控件的文字大小。
setTextColor:设置指定TextView或Button控件的文字颜色。
setTextViewCompoundDrawables:设置指定TextView或Button控件的文字周围图标。
setImageViewResource:设置ImageView或ImgaeButton控件的资源编号。
setImageViewBitmap:设置ImageView或ImgaeButton控件的位图对象。
setChronometer:设置计时器信息。
setProgressBar:设置进度条信息,包括最大值与当前进度。
setOnClickPendingIntent:设置指定控件的点击响应动作。
完成RemoteViews对象的构建与设置后调用Notification对象的setContent方法,即可完成自定义通知的定义。

2.示例

  1. 自定义通知的布局文件。
<?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. 使用自定义通知的代码。
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);
    }

猜你喜欢

转载自blog.csdn.net/weixin_43219615/article/details/100046209