Notification通知

2015/11/23

int notificationId=1;
...
{
Button btn_send = (Button) this.findViewById(R.id.send_notice);
        btn_send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification = new Notification(R.drawable.ic_launcher,"1",System.currentTimeMillis());//"1"为通知栏的内容
Intent intent = new Intent(MainActivity.this,Second_activity.class);
                PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
/*pi为当用户进入下拉页面点击item时的意图,实例化pi时第二个参数一般不用,设置为0即可*/               notification.setLatestEventInfo(MainActivity.this, "title", "content", pi);
                notificationManager.notify(notificationId,notification);
//nitify(,);第一个参数唯一,如果不做处理,该通知只能显示一次
notificationId++;
//让通知的id++,这样可以显示多次
            }
        });
}

效果
这里写图片描述
这里写图片描述

当点击tiem进入到第二个页面时,设备顶部的图片通知也不会消失,需要在第二个页面做处理

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(1);
        /*cancel()的参数为当时创建notification的id*

2016/05/25更新

RemoteViews 是 通知栏 点击展开的内容
RemoteViews是自定义的,即自定义通知栏!

这里写图片描述



            Notification notification = new Notification();
            notification.icon = R.drawable.ic_launcher;
            notification.tickerText = "tickerText"; // 手机屏幕顶部出现的文字
            notification.when = System.currentTimeMillis();
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            //
            RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.dialog);
            remoteViews.setTextViewText(R.id.msg, "to msg");
            PendingIntent pendingIntent2 = PendingIntent.getActivity(MainActivity.this, 0, intent, 
            PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.msg, pendingIntent2);
            notification.contentView = remoteViews;
            notification.contentIntent = pendingIntent2;
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(2, notification);

更新2016年9月19日

使用SDK23版本编译

notification.setLatestEventInfo(MainActivity.this, "title", "content", pi);

将编译不过,找不到该方法。
解决方案:http://blog.csdn.net/BruceHurrican/article/details/50571522

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值