当有未接电话或者短信时,在手机顶部状态栏就会出现一个小图标,提示用户没有处理的快讯,android提供了NotificationManager来管理状态栏信息,提供Notification处理这些快讯信息
示例
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/hello" /><Button android:text="Button01" android:id="@+id/Button01"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>


main2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="多谢使用" /></LinearLayout>


Activity.java
public class Test_Notification extends Activity {      /** Called when the activity is first created. */      private Button btn1;      private Notification notification;      private NotificationManager notificationManager;      private Intent intent;      private PendingIntent pendIntent;        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          btn1 = (Button) this.findViewById(R.id.Button01);          notificationManager = (NotificationManager) this                  .getSystemService(NOTIFICATION_SERVICE);// 获取系统服务(消息管理)          // 点击通知时转移内容          intent = new Intent(this, Activity2.class);          // 设置点击通知时显示内容的类         // 和Intent不同,构造pendIntent是Nofication.setLastestEventInfo参数需要        pendIntent = PendingIntent.getActivity(this, 0, intent, 0);          notification = new Notification();          btn1.setOnClickListener(new OnClickListener() {                @Override              public void onClick(View v) {                  // TODO Auto-generated method stub                  notification.icon = R.drawable.img1;// 设置在状态栏显示的图标                  notification.tickerText = "Button1通知内容.......";//设置在状态栏显示的内容                  notification.defaults = Notification.DEFAULT_SOUND;// 默认的声音                  // 设置通知显示的参数                  notification.setLatestEventInfo(Test_Notification.this,                          "Button1", "Button1通知", pendIntent);                  notificationManager.notify(0, notification);// 执行通知.              }            });      }  }  

更多相关文章

  1. Android(安卓)沉浸式状态栏归纳
  2. Android(安卓)自定义控件
  3. Android学习笔记7&s3c2440学习系列8
  4. Android(安卓)7.0 Audio :通话中的音频调用接口
  5. Android实用小技巧(持续更新)
  6. eclipse 中设置android emulator 选项
  7. Android(安卓)TextView当中设置超链接、颜色、字体
  8. 无法在Android(安卓)Studio中设置断点
  9. Android悬浮通知无效的问题

随机推荐

  1. 记录Android开发中遇到的一些问题
  2. Android(安卓)ADB简单命令入门及原理介绍
  3. [Android Memory] Android Zipalign zip
  4. Android blueZ HCI(一个):hciconfig实施和经
  5. Android 5.1部分apk的升级或缓存功能无效
  6. 【视频课程】Android底层开发关键技术—A
  7. TextView 图文聊天
  8. Android内核和驱动程序
  9. android Handler详细使用方法实例
  10. Android中DVM和java中的JVM区别