Android Notification RemoteViews

参考
Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
Android中Notification的PendingIntent无效问题解决
android的Notifications的例子demo

一、概念
  • 拿到NotificationManager
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  • 创建Notification
    Notification notification = new Notification( R.drawable.icon,"This is ticket text",System.currentTimeMillis());
    第一个参数用于指定通知图标。第二个参数用于指定通知的ticker内容,当通知刚被创建时,会在状态栏一闪而过。第三个参数指定通知创建时间,当下拉系统状态栏时,这个时间会显示在相应的通知上。
  • 指定通知的实际标题和内容。
    notification.setLatestEventInfo(context,"This is content title","This is content text",null);
  • 发出通知
    manager.notify(1,notification);//第一个参数表示通知id不能重复
  • 点击通知跳转到NotificationActivity
    使用延时执行的intent,即PendingIntent
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context,"This is content title","This is content text",pi);//第四个参数
manager.notify(1,notification);
  • 打开NotificationActivity时,要关闭通知栏。
public class NotificationActivity extends Activity{
   protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.notification_layout);
      NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
      manager.cancel(1);//nofify中第一个参数使用的id
   }
}
  • 为通知加上声音和振动,LED闪烁
    可以直接使用通知的默认效果notification.defaults = Notificaion.DEFAULT_ALL;
Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg"));
notification.sound = soundUri;
long[] vibrates = {0,1000,1000,1000};//静止时长,振动时长,静止时长……
notification.vibrate = vibrate;
notification.ledARGB = Color.GREEN;
notification.ledOnMS = 1000;//亮起时长
notification.ledOffMS = 1000;//暗去时长
notification.flags = Notificaion.FLAG_SHOW_LIGHTS;
二、 新版本变化

参考Android技巧小结之新旧版本Notification
在较新的版本中(API level > 11),Notification类中的一些方法被Android声明deprecated(弃用),其实基本上相当于全部弃用了。
1.初始化

private Notification note;
//API 11以下
note = new Notification(R.drawable.ico_launcher ,
"显示于屏幕顶端状态栏的文本", System.currentTimeMillis());
//API 11及以上
Notification.Builder builder = new Notification.Builder(nowContext)
.setTicker("显示于屏幕顶端状态栏的文本")
.setSmallIcon(R.drawable.ic_laucher);
//API 11以上版本中,状态栏显示的样式跟下拉通知栏中显示的样式,可以
//一起设置,就是通过Notification.Builder类来实现,这里的Builder只
//调用了两个方法来设置状态栏显示样式。

2、设置Notification在通知栏里的样式
(1)系统默认样式

//API 11以下:
note.setLatestEventInfo(nowContext, "take me to your heart", "Micheal learn to rock", pendingIntent);

//API 16及以上,build()方法要求API 16及以上
//一会API 11以上,一会API16以上,我也很想知道Android的API是怎么设计的
note = builder.setContentIntent(pendingIntent).setContentTitle("title").setContentText("text").build();

(2)自定义样式:
自定义样式,就是让Notification在通知栏显示成自定义的xml布局.应当注意的是,Notification的自定义样式,只支持以下可视组件:
FrameLayout, LinearLayout, RelativeLayout.TextView, Button, AnalogClock, ImageView, ImageButton, Chronometer, ProgressBar

RemoteView view = new RemoteView(nowActivity.getPackageName(), R.layout.note_layout);
//API 11以下
note.contentView = view;
note.contentIntent = pendingIntent;
//API 16及以上,又是build()方法导致的,汗。。
note = builder.setContent(view).setContentIntent(pendingIntent).build();

这个步骤里有一个很值得注意的地方:pendingIntent被设置为note的contentIntent的值,就意味着点击了这个通知才会触发该Intent。
那么如果只是想让自定义布局里的某个按钮触发呢?比如说,弄了一个音乐播放器,Service负责播放音乐,Notification显示当前播放进度和一些简单的暂停按钮、上一首、下一首按钮,让用户不用再打开界面就可以通过Notification上的按钮操纵音乐播放。假设说想让自定义布局里的一个id为R.id.button1的按钮来触发这个Intent,可以如下操作:

view.setOnClickPendingIntent(R.id.button1, pendingIntent);
//在上面创建RemoteView实例后加上这句

然后注意,pendingIntent已经绑定到按钮上了,上面Notificatiion实例中,设置contentIntent的语句要去掉。

三、简单例子

参考
Android通知Notification入门小例子(一)

android notification.gif

package com.demo.notificationdemo;

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 start_notification;

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

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.start_notification:
            /**
             * 创建通知管理类NotificationManager的实例,用来管理通知
             */
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            /**
             * 创建通知类Notification实例(用来存储通知所需的信息); 一共三个参数:
             * 1)、指定通知使用的图标,如:R.drawable.ic_launcher ;
             * 2)、指定通知的ticker内容,通知被创建的时候,在状态栏一闪而过,属于瞬时提示信息。
             * 3)、指定通知被创建的时间,以毫秒为单位,下拉状态栏时,这个时间会显示在相应的通知上。
             */
            Notification notification = new Notification(
                    R.drawable.ic_launcher, "有通知来了", System.currentTimeMillis());

            /**
             * 实例化Intent,构建意图:从当前页面跳转到NotificationActivity页面
             */
            Intent intent = new Intent(this, NotificationActivity.class);

            /**
             * 用于启动活动、启动服务以及发送广播等。 根据需求来选择是使用
             * getActivity()方法、getBroadcast()方法、还是 getService() 方法。 一共四个参数:
             * 1)、第一个参数是 上下文Context ; 2)、 第二个参数一般用不到,通常都是传入 0 即可 3)、第三个参数是一个
             * Intent对象,我们可以通过这个对象构建出 PendingIntent 的“意图”; 4)、第四个参数用于确定
             * PendingIntent 的行为
             */
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    intent, PendingIntent.FLAG_CANCEL_CURRENT);

            /**
             * 设定通知标准的布局。四个参数: 1)、上下文对象Context; 2)、指定通知的标题内容,下拉系统状态栏可看到;
             * 3)、指定通知的正文,下拉系统状态栏可看到; 4)、用于启动活动、启动服务以及发送广播
             */
            notification.setLatestEventInfo(this, "股票大涨", "今日万科的股票停牌,应对恶意收购",
                    pendingIntent);

            /**
             * 启动通知. 两个参数: 1)、id,保证每个通知的id唯一; 2)、Notification对象
             */
            manager.notify(1, notification);
            break;

        default:
            break;
        }
    }

}


package com.demo.notificationdemo;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

public class NotificationActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification_text);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //cancel()方法中的参数是我们在启动通知,调用manager.notify(1, notification)方法时,传的id
        manager.cancel(1);//取消系统状态栏上的通知图标
    }
}
四、自定义通知RemoteViews

参考Android自定义Notification并没有那么简单

RemoteViews表示的是一个View结构,它可以在其他进程中(系统的SystemServer进程)显示,为了能够更新它的界面,RemoteViews提供了一组基础操作用于跨进程更新。

Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "hello world";
notification.when = System.currentTimeMillis();
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, DemoActivity_1.class);
intent.putExtra("sid", "" + sId);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
System.out.println(pendingIntent);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);
//无法直接访问view,必须通过setTextViewText设置id和要设置的文本
remoteViews.setTextViewText(R.id.msg, "chapter_5: " + sId);
//设置ImageView的图片
remoteViews.setImageViewResource(R.id.icon, R.drawable.icon1);
PendingIntent openActivity2PendingIntent = PendingIntent.getActivity(this,0, new Intent(this, DemoActivity_2.class), PendingIntent.FLAG_UPDATE_CURRENT);
//给一个控件添加单击事件
remoteViews.setOnClickPendingIntent(R.id.open_activity2, openActivity2PendingIntent);
notification.contentView = remoteViews;
notification.contentIntent = pendingIntent;
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(sId, notification);

1.折叠式通知 使用bigContentView

// 通过RemoteViews来创建自定义的Notification视图
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.notification);
contentView.setTextViewText(R.id.textView,"show me when collapsed");

Notification notification = builder.build();
notification.contentView = contentView;

// 通过RemoteViews来创建自定义的Notification视图
RemoteViews expandedView = new RemoteViews(getPackageName(),R.layout.notification_expanded);
notification.bigContentView = expandedView;

//可以做版本判断
//if(Build.VERSION.SDK_INT >= 16){
//  notification.bigContentView = rvMain;
//}

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID_COLLAPSE, notification);

2.悬挂式通知 使用setFullScreenIntent
android5.0新增加的方式,可以在屏幕上方产生通知且不打断用户操作

    public void headsupNotify(View view) {
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setContentTitle("Headsup Notification")
                .setContentText("I am a Headsup notification.");

        Intent push = new Intent();
        push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        push.setClass(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this, 0, push, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentText("Heads-Up Notification on Android 5.0")
                .setFullScreenIntent(pendingIntent, true);

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

3.显示等级的通知 使用setVisibility

    public void visibilityNotify(View view) {
        RadioGroup radioGroup = (RadioGroup) findViewById(
                visibility_radio_group);
        Notification.Builder builder = new Notification.Builder(this)
                .setContentTitle("Notification for Visibility Test");
        switch (radioGroup.getCheckedRadioButtonId()) {
            case R.id.radio_button_public:
                //任何情况下都显示通知
                builder.setVisibility(Notification.VISIBILITY_PUBLIC);
                builder.setContentText("Public");
                builder.setSmallIcon(R.drawable.ic_public);
                break;
            case R.id.radio_button_private:
                //只有当没锁屏时显示
                builder.setVisibility(Notification.VISIBILITY_PRIVATE);
                builder.setContentText("Private");
                builder.setSmallIcon(R.drawable.ic_private);
                break;
            case R.id.radio_button_secret:
                //在pin,password等安全锁和没有锁屏的情况下才显示
                builder.setVisibility(Notification.VISIBILITY_SECRET);
                builder.setContentText("Secret");
                builder.setSmallIcon(R.drawable.ic_secret);
                break;
        }
        NotificationManager nm = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
        nm.notify(NOTIFICATION_ID_VISIBILITY, builder.build());
    }

e

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

推荐阅读更多精彩内容