参考
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

更多相关文章

  1. Android(安卓)在程序界面上显示图片
  2. android amlogic系统源码中去除屏保
  3. Android(安卓)WebView
  4. TextView的众多使用
  5. ImageVIew 设置图片大小
  6. No 122 · android获取本机号码和本机唯一标识
  7. android 键盘 搜索键
  8. Android(安卓)屏幕设置
  9. Android(安卓)5.0有哪些变化

随机推荐

  1. 远程调试android
  2. Android SeekBar样式修改实现签到效果
  3. android中获取context的多种方法的区别(th
  4. android开发系列之数据存储
  5. Android中在sdcard上创建文件夹,注意加权
  6. Android翻页效果原理实现之引入折线
  7. Android 获取imei号码,获取手机型号和系统
  8. Android 读取通信录号码
  9. Android输入框格式化Edittexthelp(有项目
  10. android 从EditText中获取内容