一手遮天 Android - Notification: 自定义 ui 的 Notification

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - Notification: 自定义 ui 的 Notification

示例如下:

/notification/NotificationDemo2.java

/**
 * Notification
 *
 * 本例用于演示
 * 1、弹出自定义 ui 的 Notification
 */

package com.webabcd.androiddemo.notification;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

import com.webabcd.androiddemo.R;

public class NotificationDemo2 extends AppCompatActivity {

    private Button mButton1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_notificationdemo2);

        mButton1 = findViewById(R.id.button1);

        sample();
    }

    private void sample() {
        final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder notificationBuilder = null;
        if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String channel_id = "channel_id";
            String channel_name = "channel_name";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel notificationChannel = new NotificationChannel(channel_id, channel_name, importance);
            notificationManager.createNotificationChannel(notificationChannel);
            notificationBuilder = new Notification.Builder(this, channel_id);
        } else {
            notificationBuilder = new Notification.Builder(this);
        }

        // 普通的自定义通知栏(高度最大 64dp)
        // 只能通过 setTextViewText(), setViewVisibility(), setProgressBar() 之类的修改控件信息
        // 可以通过 setOnClickPendingIntent() 为指定控件增加点击后的动作
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.remoteviews_notification_notificationdemo2);
        remoteViews.setTextViewText(R.id.textView1, "aaaaaa");

        // 大个的自定义通知栏(高度最大 256dp)
        // 只能通过 setTextViewText(), setViewVisibility(), setProgressBar() 之类的修改控件信息
        // 可以通过 setOnClickPendingIntent() 为指定控件增加点击后的动作
        RemoteViews remoteViewsBig = new RemoteViews(this.getPackageName(), R.layout.remoteviews_big_notification_notificationdemo2);
        remoteViewsBig.setTextViewText(R.id.textView1, "111111");


        final Notification notification = notificationBuilder
                .setSmallIcon(R.mipmap.ic_launcher_alpha)

                // api level 24 以下系统指定自定义通知栏
                .setContent(remoteViews)

                // api level 24 或以上系统指定自定义通知栏(普通的自定义通知栏)
                // .setCustomContentView(remoteViews)

                // api level 24 或以上系统指定自定义通知栏(大个的自定义通知栏)
                // .setCustomBigContentView(remoteViewsBig)

                .build();

        // 弹出通知
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notificationManager.notify(111, notification);
            }
        });
    }
}

/layout/activity_notification_notificationdemo2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="弹出自定义 ui 的 Notification"
        android:textAllCaps="false" />

</LinearLayout>

/layout/remoteviews_notification_notificationdemo2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="@color/blue"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white" />

</LinearLayout>

/layout/remoteviews_big_notification_notificationdemo2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:background="@color/orange"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white" />

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-06-03 08:09  webabcd  阅读(448)  评论(0编辑  收藏  举报