在通知栏弹出发出的消息;类似于接受信息等等的一些小demo;

需要的activity_main.xml


[html] view plain copy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <Button
  6. android:id="@+id/send"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="发送通知"/>
  10. <Button
  11. android:id="@+id/clear"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="清除通知"/>

一个小demo。点击 发送通知 按钮,则发送通知到设备的通知栏。点击 清除通知 则清除通知栏上的消息通知。


[java] view plain copy
  1. packagezhangphil.notification;
  2. importandroid.os.Bundle;
  3. importandroid.view.View;
  4. importandroid.widget.Button;
  5. importandroid.app.Activity;
  6. importandroid.app.Notification;
  7. importandroid.app.NotificationManager;
  8. importandroid.support.v4.app.NotificationCompat;
  9. publicclassMainActivityextendsActivity{
  10. privatefinalintNOTIFICATION_ID=105;
  11. @Override
  12. protectedvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. Buttonsend=(Button)findViewById(R.id.send);
  16. send.setOnClickListener(newView.OnClickListener(){
  17. @Override
  18. publicvoidonClick(Viewv){
  19. sendNotification();
  20. }
  21. });
  22. Buttonclear=(Button)findViewById(R.id.clear);
  23. clear.setOnClickListener(newView.OnClickListener(){
  24. @Override
  25. publicvoidonClick(Viewv){
  26. clearNotification();
  27. }
  28. });
  29. }
  30. privatevoidsendNotification(){
  31. NotificationManagernotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  32. NotificationCompat.BuildermBuilder=newNotificationCompat.Builder(
  33. this);
  34. mBuilder.setSmallIcon(R.drawable.ic_launcher);
  35. mBuilder.setContentTitle("通知的标题");
  36. mBuilder.setContentText("通知的内容");
  37. Notificationnotification=mBuilder.build();
  38. //缺省设置为当发送通知到通知栏时候:提示声音+手机震动
  39. notification.defaults=Notification.DEFAULT_SOUND
  40. |Notification.DEFAULT_VIBRATE;
  41. //通知的时间
  42. notification.when=System.currentTimeMillis();
  43. //发送到手机的通知栏
  44. notificationManager.notify(NOTIFICATION_ID,notification);
  45. }
  46. privatevoidclearNotification(){
  47. NotificationManagernotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  48. notificationManager.cancel(NOTIFICATION_ID);
  49. }
  50. }

更多相关文章

  1. Android(安卓)使用Socket实现服务器与手机客户端的长连接一:一对
  2. Android设置中清除应用数据源码分析
  3. Android(安卓)IQ包 发送 基于XMPP
  4. Android(安卓)Handler(四)
  5. Android(安卓)USB Host与HID通讯(二)
  6. SpringBoot webSocket 发送广播、点对点消息,Android接收
  7. Android定义定时器实现定时任务(亲测可用)
  8. android常用的API接口调用
  9. Android(安卓)Handler机制5--消息发送

随机推荐

  1. 为什么Spring的JDBC模板不使用表的默认值
  2. Spring 3.0将文件注入资源
  3. 2009年1月19日,欢迎PyLucene加入Lucene子
  4. 测试技术大牛谈成长经历:一个好的软件测试
  5. Javascript 检查字符串是否是数字的几种
  6. JavaEE入门手把手教你做简单登陆界面(1)
  7. 填充java fx表时出现空指针异常
  8. Android服务端开发1-使用Eclipse搭建Java
  9. java高并发synchronized学习
  10. #研究JAVAAPI系列--Number类+BigDecimal