消息通知之Notification与PendingIntent<一>

Notification-----状态栏信息通知

一.简意:

(1)消息通知:当某些应用有消息时,在手机最上方的状态栏会有一条通知

二,基本用法:

(1)获取服务:通过getSystemService来获取NotificationManager服务

NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

(2)设置点击通知信息后跳转的Activity,如果你不需要则忽略此步骤(对应的在notification也不需要设置ContentIntent):

利用PendingIntent来发送:

  ---创建intent对象:

Intent intent = new Intent(this , NotificationActivity.class);
 -----然后创建PendingIntent对象:

        //4个参数:context,0,intent,确定PendingIntent的行为
        //PendingIntent.getActivity(),getBroadcast(),getService()等方法
        PendingIntent pendingIntent = PendingIntent.getActivity(this , 0 , intent , 0);

看一下,PendingIntent的源码,如下:前面还有一大堆解释,点击getActivity,自己去看一下

大概就是:上下文对象,请求码(基本传0),Intent对象,flag标签

    public static PendingIntent getActivity(Context context, int requestCode,
            Intent intent, @Flags int flags) {
        return getActivity(context, requestCode, intent, flags, null);
    }
flag标签值(一般可传0):可选4种值

                    FLAG_ONE_SHOT,
                    FLAG_NO_CREATE,
                    FLAG_CANCEL_CURRENT,
                    FLAG_UPDATE_CURRENT,

(3)设置通知,为了更好的兼容性,可以调用support-v4的NotificationCompat来设置

Notification notification = new NotificationCompat.Builder(context).build();

        Notification notification = new NotificationCompat.Builder(this)
                //设置标题
                .setContentTitle("你好6")
                //设置内容
                .setContentText("为什么你会好6呢?原因是....")
                //设置时间
                .setWhen(System.currentTimeMillis())
                //设置小图标
                .setSmallIcon(R.mipmap.p1)
                //设置大图标,当系统下拉时就会看到大图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources() , R.mipmap.t2))
                //设置点击时跳转的PendingIntent
                .setContentIntent(pendingIntent)
                //设置点击通知后,通知取消,也可以在代码中去取消
               // .setAutoCancel(true)
                .build();
以上是常用方法:

.设置标题,内容,时间,颜色,小图标,大图标,跳转,点击后取消通知,等等

当然,最后一定要.build()方法

(4)调用NotificationManager的notify()方法来显示:

源码:两个参数,第一个是id,第二个是Notificatioin对象(这个id,当我们点击该通知需要获取数据时,可能会用到)

    public void notify(int id, Notification notification)
    {
        notify(null, id, notification);
    }
如:

        //第一个参数id,第二个Notification对象
        manager.notify(1 , notification);
(5)创建点击跳转的Activity,当然,你若不需要跳转,可以忽略

可以在onCreate方法中,增加点击后取消通知的方法(如果你没有在发送时设置setAutoCancel方法的话)

    private void cancleNotification() {
        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1);//取消通知,参数是,发送时的id
    }
(6)试一下:效果

点击发送按钮后:


下拉:


添加通知,跳转到另一个Activity,截图略

三,其他用法:

(1)设置震动,当通知时会震动:setVibrate()

                //设置震动:参数一个long[]{}数组,里面可以依次设置:静止时长,震动时长,静止时长,震动时长....
                .setVibrate(new long[]{0, 1000 , 1000 , 1000})
需要添加震动权限:

<uses-permission android:name="android.permission.VIBRATE"/>
(2)设置闪烁效果:

                //设置闪烁灯:3个参数:颜色,灯亮时长,灯暗时长
                .setLights(Color.YELLOW , 1000 , 1000)
特别是当手机锁屏后效果明显

(3)设置播放铃声:当然这个用户一般都不会喜欢发条通知还播放音乐

              //设置发出通知时播放的音乐
               // .setSound(Uri.parse())
(4)设置超长文本或者图片时,若果需要全部显示出来,可以用:setStyle()方法,接受Style对象

如;

.setStyle(new NotificationCompat.BigTextStyle().bigText("str"))
设置一个字符串文本,当然也可以传入图片对象

                //设置超大图片
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(
                        getResources() , R.mipmap.p1
                )))
因为bigPicture()接收一个Bitmap对象,故而...

(5)设置通知重要性:有5个选项

                //设置通知的重要性:5个值,从低到高
                //public static final int PRIORITY_MIN = -2;    //特定效果才通知
                //public static final int PRIORITY_LOW = -1;    //缩小或者位置靠后
                //public static final int PRIORITY_DEFAULT = 0; //默认
                //public static final int PRIORITY_HIGH = 1;    //放大或者位置靠前
                //public static final int PRIORITY_MAX = 2;     //独立通知
                .setPriority(NotificationCompat.PRIORITY_MAX)
(6)当然,如果你不想设置这些东东,但是想要铃声啊等效果,可以 设置默认

                //设置默认效果:不需要自己设置音乐铃声,等等
                //.setDefaults(NotificationCompat.DEFAULT_ALL)

四.PendingIntent:

(1)intent是随着Activity而消失的,而PendingIntent可以看做是对Intent的包装,是延迟的intent

(2)通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例

(3)常用情况:状态栏通知,发送短信等等

(4)主要作用:用来在某个事件完成后执行特定的Action

如:

发送短信(通知例子上面有了)

定义动作:

    //定义动作
    private final static String SEND_ACTION      = "send";
    private final static String DELIVERED_ACTION = "delivered";
创建SmsManager和PendingIntent对象:(2个deliveredPI用于对方接收到后通知你对方已经接收)

        //获取SmsManager管理器
        SmsManager s = SmsManager.getDefault();
        //创建PendingIntent对象:4个参数:context,0,intent,flag
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SEND_ACTION),
                PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED_ACTION),
                PendingIntent.FLAG_CANCEL_CURRENT);
注册发送:

        // 发送完成
        registerReceiver(new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "发送成功!", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Send Failed because generic failure cause.",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "Send Failed because service is currently unavailable.",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Send Failed because no pdu provided.", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Send Failed because radio was explicitly turned off.",
                                Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(getBaseContext(), "Send Failed.", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SEND_ACTION));
注册通知你对方接收成功(当对方接收时调用)

        // 通知你对方已经接收
        registerReceiver(new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Delivered Success!", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(getBaseContext(), "Delivered Failed!", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(DELIVERED_ACTION));
利用SmsManager来启动发送:

 s.sendTextMessage(receiver, null, text, sentPI, deliveredPI);

当然还有很多很多其他的....










  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值