2018-06-05-Notification

Notification 通知栏

Notification是在应用常规界面之外展示的消息。当app让系统发送一个消息的时候,消息首先以图标的形式显示在通知栏中。要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看。通知栏和通知抽屉都是系统曾main控制的,你可以随时查看,不限制于App。

Notication的创建

public class MainActivity extends Activity {


    private static final int NID_1 = 0x1;

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

    }

    public void sendnotification1(View v){
        //API 11以后
//        Notification.Builder builder = new Notification.Builder(this);
        //v4支持包
        NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
        //设置相关属性
        builder1.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
        builder1.setContentTitle("你有一条新的消息");
        builder1.setContentText("你好啊");

        //创建一个通知对象
        Notification n = builder1.build();

        //获取系统的通知管理器,并发送
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(NID_1,n);

    }

}

notification的其他属性

//其他不必要的属性
        builder1.setAutoCancel(true);
        builder1.setDefaults(Notification.DEFAULT_ALL);  //声音、震动、呼吸灯
        builder1.setNumber(10);
        builder1.setTicker("新消息"); //没下拉的时候显示的字
        builder1.setOngoing(true); //设置为常驻通知

利用Intent,通过notification打开另一个Activity

public class MainActivity extends Activity {


    public static final int NID_1 = 0x1;

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

    }

    public void sendnotification1(View v){
        //API 11以后
//        Notification.Builder builder = new Notification.Builder(this);
        //v4支持包
        NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
        //设置相关属性
        builder1.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
        builder1.setContentTitle("你有一条新的消息");
        builder1.setContentText("你好啊");

        //其他不必要的属性
        builder1.setAutoCancel(true);
        builder1.setDefaults(Notification.DEFAULT_ALL);  //声音、震动、呼吸灯
        builder1.setNumber(10);
        builder1.setTicker("新消息"); //没下拉的时候显示的字
        builder1.setOngoing(true); //设置为常驻通知

        //定义一个意图,当店家通知时要打开一个界面
        Intent intent = new Intent(this,Main2Activity.class);
        //参数:上下文,请求编码,意图,创建PendingIntent的方式
//        PendingIntent.FLAG_CANCEL_CURRENT   :取消当前的PI,创建新的
//        PendingIntent.FLAG_NO_CREATE :如果有就使用,没有就不创建
//        PendingIntent.FLAG_ONE_SHOT :只是用一次
//        PendingIntent.FLAG_UPDATE_CURRENT   :如果有,更新Intent,没有就创建
        PendingIntent pi = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        //通知的事件
        builder1.setContentIntent(pi);

        //创建一个通知对象
        Notification n = builder1.build();

        //获取系统的通知管理器,并发送
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(NID_1,n);

    }

}

另一个被打开的Activity

public class Main2Activity extends AppCompatActivity {

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

        //打开界面后取消指定ID的通知
        //Notification是系统层的服务,从哪里都可以获取到
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.cancel(MainActivity.NID_1);
    }

}

第二个Activity中取消notification和第一个Activity中

builder1.setAutoCancel(true);

这里的效果一样,原因是notification是系统层的服务,从哪里都可以获取

大视图的notification

大视图的东西样式设置一般在Style中,普通设置(比如说setContentTitle、setContentText)在builder

 //API 11以后
//        Notification.Builder builder = new Notification.Builder(this);
        //v4支持包
        NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
        //设置相关属性
        builder1.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
        builder1.setContentTitle("消息");
        builder1.setContentText("消息");

        //新建样式
        NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
        style.setBigContentTitle("吟诗作对");
        style.addLine("长亭外");
        style.addLine("古道边");
        style.addLine("一行白鹭上青天");

        builder1.setStyle(style);

        Notification n = builder1.build();
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(NID_2,n);

显示进度条通知

线程模拟发送进度

public void sendnotification3(View v){
        //API 11以后
//        Notification.Builder builder = new Notification.Builder(this);
        //v4支持包
        final NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
        //设置相关属性
        builder1.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
        builder1.setContentTitle("更新中..");
        builder1.setContentText("正在升级");
        builder1.setProgress(100,5,false); //总进度,现在进度,确定的

        final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(NID_3,builder1.build());


        //模拟更新的线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int progress = 0; progress <= 100; progress+=5){
                    NotificationCompat.Builder builder = builder1.setProgress(100, progress, false);
                    nm.notify(NID_3, builder1.build());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                builder1.setProgress(0,0,false);
                builder1.setContentText("更新完成");
                nm.notify(NID_3,builder1.build());
            }
        }).start();

    }

}

自定义通知视图

  • 通知的框架允许自己去定义同志的布局。通过REmoteViews对象来定义通知的外观,自定义的通知布局与常规通知类似,但是它是基于定义在xml文件的RemoteViews对象来操作的。
  • 自定义通知的可用高度取决于通知视图的。正常的视图布局高度限制在256dp.
  • 为了去定义自己的通知布局,从扩充xml文件获取一个RemoteViews对象的实例展开开始。然后类似于调用setContentTitle方法一样,我们呢需要调用setcontent()。为了能设置更多的细节,我们使用RemoteViews对象的方法来设置更多的内容。
public void sendnotification4(View v){
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        //创建一个远程的视图
        RemoteViews views = new RemoteViews(getPackageName(),R.layout.custom_layout);
        builder.setContent(views);
        builder.setTicker("音乐");
        builder.setOngoing(true);
        builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标

        //view显示的其他内容
        //button也是继承
        views.setTextViewText(R.id.button,"暂停");
//        views.setOnClickPendingIntent();  按钮的单击事件,通过Intent启动另一个Actiity

        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(NID4,builder.build());
    }

自定义的布局文件

<?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">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:maxWidth="64dp"
        android:maxHeight="64dp"
        android:src="@drawable/ic_launcher"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview"
        android:text="歌曲名称"
        android:layout_weight="1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" />


</LinearLayout>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容