Notification(通知) 简单用法

Notification(通知) 是应用程序提醒用户某件事情已经发生了的一种方式,可以在“状态栏”和“通知托盘”中看到它。如我们更新程序的时候,可以通过Notification来实现下载进度。

Notification 可以有以下动作来增强用户提醒:

1.在状态栏中显示图标。

2.灯光:手机LED呼吸灯闪烁

3.发出声音提醒。

4.手机震动。

5.在通知托盘中显示更多的信息

一,创建Notification

Notification需要使用NotificationManager来管理。NotificationManager是用来处理Notification的系统服务。可以使用getSystemService来获取。创建Notification有两种方式

第一种:使用Notification实例

    //获取NotificationManager的引用
        final NotificationManager nManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
        //创建Notification
        //第一个参数:在状态栏中显示的图标,
        //第二个参数:在状态栏中显示的文本
        //第三个参数:时间戳,通知显示的时间,NotificationManager会按照这个时间来排序Notification
        final Notification notifi = new Notification(R.drawable.ic_launcher,"我的通知",System.currentTimeMillis());
        notifi.contentView=new RemoteViews(this.getPackageName(), R.layout.nview);
        
        Button btn2 = (Button) findViewById(R.id.Button01);
        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //第一个参数:Notification的ID,如果ID相同那么会更新之前的Notification
                //第二个参数:Notification的实例
                nManager.notify(1, notifi);
            }
        });
        

第二种:使用NotificationBuilder(推荐这种方式)

        //获取NotificationManager的引用
        final NotificationManager nManager2 = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
        //使用Notification Builder创建通知
        Notification.Builder builder = new Notification.Builder(MainActivity.this);
        //设置通知在状态栏中的图标
        builder.setSmallIcon(R.drawable.ic_launcher);
        //通知在状态栏中显示的文本
        builder.setTicker("第一个");
        final Notification nf = builder.getNotification();
        //上面和下面这个方法所需的API版本不一样。功能是一样的
        //final Notification nf2 = builder.build();
        
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                nManager2.notify(1, nf);
            }
        });

二。Notification自定义View,点击自定义View中的按钮触发事件

可以使用RemoteViews来给Notification指定自定义View

NotificationManager nManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
Notification notifi = new Notification.Builder(this).build();
notifi.tickerText = "我的Notification";
notifi.when = System.currentTimeMillis();
notifi.icon = R.drawable.ic_launcher;
// 使用RemoteViews来给Notification指定自定义View
notifi.contentView = new RemoteViews(this.getPackageName(),
R.layout.nview);

// 点击自定义布局中的"打开相机“按钮打开相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);
//给自定义View指定PendingIntent
notifi.contentView.setOnClickPendingIntent(R.id.button2,pi);

// 发送通知
nManager.notify(1, notifi);

nview.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="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开网页" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开照相机" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开电话本" />

</LinearLayout>

Notification 还有更多的属性。可以参考API 。随着android api 版本的不同。对应的方法也会有些许修改。关于Notification的手机震动和LED灯,音效等。需要在AndroidMainfest.xml 中添加 震动,闪光等权限。有些手机ROM被优化过。可能效果不正确。

原文地址:https://www.cnblogs.com/ywtk/p/3836244.html