Android笔记:Notification的使用总结

关于Android通知栏Notification使用的总结。

重点知识点:

  • 使用Notification.builder类的系列set方法设置通知栏的参数;
  • setContentIntent()方法添加PendingIntent达到点击启动Activity的功能。
  • 调用builder.build()方法返回一个notification对象;
  • 然后利用notification的一些属性设置它的声音,震动,灯光,点击关闭等等特性。
    Android通知栏Notification使用的总结

notification点击后消失两种方法:

  1. 在点击启动的新Activity中调用notificationManager.cancel();
  2. 给notification.flags = Notification.FLAG_AUTO_CANCEL;

notification常驻状态栏的两个flag

  1. FLAG_ONGOING_EVENT //提示用户程序正在运行,如正在通话……
  2. FLAG_NO_CLEAR //clear按钮将无法清除这个notification

案例代码:

package com.m1910.notificationtest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

    private Button sendNotice;
    String tickerText = "This is ticker text.";
    String contentTitle = "This is content title.";
    String contentText = "This is content Text.";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendNotice = (Button) findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.send_notice:
            // 实例化一个NotificationManager对象。
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            // 创建intent用来启动NotificationActivity
            Intent intent = new Intent(this, NotificationActivity.class);
            // 将intent传入到PendingIntent对象中用getActivity()方法实例化一个PendingIntent对象。
            // 这里还可以使用getService()以及getBroadcast()方法,传入的参数一样。
            PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            // 实例化一个Builder对象。
            Notification.Builder nBuilder = new Notification.Builder(this);
            // 利用Builder对象的一系列set方法设置参数,其中注意使用setContentIntent()传入上面的PendingIntent对象pi;
            // builder()方法返回一个Notification对象
            Notification notification = nBuilder.setContentTitle(contentTitle).setContentText(contentText)
                    .setTicker(tickerText).setSmallIcon(R.drawable.ic_launcher).setContentIntent(pi).build();
            // 设置提示音
            notification.defaults = Notification.DEFAULT_SOUND;
            // 设置点击后自动清除通知
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            // 调用NotificationManager的notify()方法启动这个notification;
            // 第一个参数APP中这个notification唯一标识符,第二个是要显示的Notification对象。
            manager.notify(1, notification);
            break;
        default:
            break;
        }
    }
}

Notification进阶部分:

  1. 如网易云音乐那样的可操作的通知栏需要用到相对比较复杂的布局使用Notification.BuildersetContent(RemoteViews views)设置自定义布局,暂时还没做过;
  2. android4.1之后,有小布局,大布局,下滑后的通知栏,选中的状态栏,以及锁屏状态的通告,这些都是可以深入研究的方向。
  3. 前台服务的使用,创建服务的时候调用startForeground()方法启动notification,让服务在状态栏常驻。
Android笔记:Notification的使用总结

原文链接:https://beltxman.com/1251.html,若无特殊说明本站内容为 行星带 原创,未经同意禁止转载。

Android笔记:Notification的使用总结”上有 8 条评论;

评论已关闭。

Scroll to top