Android基础教程之---Android状态栏提醒(Notification,NotificationManager)的使用!

来源:互联网 发布:centos 不支持chrome 编辑:程序博客网 时间:2024/06/13 04:42

大家好今天简单讲一下Android状态栏提醒,这个在开发中也会经常使用,当我们插上USB会有状态栏提醒,来短信时也会有状态栏的提醒。

而在Android中有提醒功能的也可以用AlertDialog,但是我们要审重的使用,因为当使用AlertDialog 的时候,用户正在进行的操作将会被打断

因为当前焦点被AlertDialog得到。我们可以想像一下,当用户打游戏正爽的时候,这时候来了一条短信。如果这时候短信用AlertDialog提醒,用户必须先去处理这条提醒,从而才能继续游戏。用户可能会活活被气死。而使用Notification就不会带来这些麻烦事,用户完全可以打完游戏再去看这条短信。所以在开发中应根据实际需求,选择合适的控件。

好了我今天又简单写了一个Demo, 教大家如何使用Notification,大致分以下几个步骤:

第一步:新建一个Android工程命名为NotificationDemo.

第二步:修改main.xml代码如下:

[java] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="Welcome to Mr Wei's blog"
  11. />
  12. <Button
  13. android:id="@+id/showButton"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text="showNotification"
  17. />
  18. <Button
  19. android:id="@+id/cancelButton"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:text="cancelNotification"
  23. />
  24. </LinearLayout>

第三步:修改NotificationDemo.java代码如下:

[java] view plaincopyprint?
  1. package com.tutor.notification;
  2. import android.app.Activity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. public class NotificationDemoextends Activity implements OnClickListener{
  13. private Context mContext;
  14. private Button showButton,cancelButton;
  15. private Notification mNotification;
  16. private NotificationManager mNotificationManager;
  17. private finalstatic int NOTIFICATION_ID =0x0001;
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. setupViews();
  23. }
  24. //这里是初始化一些操作,可以看到onCreate()方法里代码非常简洁。
  25. public void setupViews(){
  26. mContext = NotificationDemo.this;
  27. showButton = (Button)findViewById(R.id.showButton);
  28. cancelButton = (Button)findViewById(R.id.cancelButton);
  29. mNotification = new Notification(R.drawable.icon,"This is a notification.",System.currentTimeMillis());
  30. //将使用默认的声音来提醒用户
  31. mNotification.defaults = Notification.DEFAULT_SOUND;
  32. mNotificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
  33. showButton.setOnClickListener(this);
  34. cancelButton.setOnClickListener(this);
  35. }
  36. //按钮点击事件响应
  37. public void onClick(View v) {
  38. if(v == showButton){
  39. Intent mIntent = new Intent(mContext,NotificationDemo.class);
  40. //这里需要设置Intent.FLAG_ACTIVITY_NEW_TASK属性
  41. mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  42. PendingIntent mContentIntent =PendingIntent.getActivity(mContext,0, mIntent,0);
  43. //这里必需要用setLatestEventInfo(上下文,标题,内容,PendingIntent)不然会报错.
  44. mNotification.setLatestEventInfo(mContext, "10086","您的当前话费不足,请充值.哈哈~", mContentIntent);
  45. //这里发送通知(消息ID,通知对象)
  46. mNotificationManager.notify(NOTIFICATION_ID, mNotification);
  47. }else if(v == cancelButton){
  48. //取消只要把通知ID传过来就OK了.
  49. mNotificationManager.cancel(NOTIFICATION_ID);
  50. }
  51. }
  52. }

第四步:运行Android工程,效果如下图所示:

OK今天就写到这里,大家晚安了~如果有什么疑问请大家留言。Thx~

原创粉丝点击