鱼C论坛

 找回密码
 立即注册
查看: 1841|回复: 1

[原创]android学习之在状态栏上显示消息通知

[复制链接]
发表于 2014-11-19 17:04:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  要想实现在状态栏上显示消息通知的话,首先我们得先得到一个通知管理器:NotificationManager
然后获得一个Builder对象,在这个对象里面设置消息的信息,设置好之后,通过builder.build()方法获得
通知对象:Notification.然后调用通知管理器:notificationManager.notify(1, notification);将通知对象以参数的形式传递进去就可以了;
其主要实现的代码如下:

  1. public void sendNotification(View v)
  2.         {
  3.                 //new Notification();
  4. //                Notification notification = new Notification();
  5. //                notification.icon = R.drawable.ic_launcher;
  6. //                notification.tickerText = "有商品更新";
  7. //               
  8. //                Intent intent = new Intent();
  9. //                intent.setClassName("com.example.video", "com.example.video.MainActivity");
  10. //                PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
  11. //               
  12. //                notification.setLatestEventInfo(this, "莫惹秋风断残雪", "清凌河月九江照,落日挥洒玉门口", pendingIntent);
  13.                
  14.                 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  15.                 Intent intent = new Intent();    //这里是一个意图对象
  16.                 intent.setClassName("com.example.video", "com.example.video.MainActivity");   这个是设置点击一个消息会跳转到那个页面
  17.                 PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);   //将意图以参数的形式设置到////这个对象里面
  18.                
  19.                 Notification.Builder builder = new Notification.Builder(this);
  20.                 builder.setAutoCancel(true);
  21.                 builder.setContentTitle("莫惹秋风断残雪");
  22.                 builder.setContentText("清凌河月九江照,落日挥洒玉门口");
  23.                 builder.setContentIntent(pendingIntent);   //将pendingIntent加入到里面,这个是设置意图内容的
  24.                 builder.setSmallIcon(R.drawable.ic_launcher);
  25.                 builder.setWhen(System.currentTimeMillis());
  26.                 Notification notification = builder.build();
  27.                 notificationManager.notify(i++,notification);   //将通知对象加到通知管理器里面
  28.                
  29.         }
复制代码

QQ截图20141119165007.png


评分

参与人数 1荣誉 +5 鱼币 +3 贡献 +2 收起 理由
拈花小仙 + 5 + 3 + 2 感谢楼主无私奉献!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-11-19 18:38:11 | 显示全部楼层
厉害  ,支持楼主
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-2 15:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表