Android状态栏提醒(Notification,NotificationManager)的使用
第一步:新建一个Android工程命名为NotificationDemo.第二步:修改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="Welcome to Mr Wei's blog" /><Button android:id="@+id/showButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="showNotification"/><Button android:id="@+id/cancelButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="cancelNotification"/></LinearLayout>第三步:修改NotificationDemo.java代码如下:view plaincopy to clipboardprint?package com.tutor.notification;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class NotificationDemo extends Activity implements OnClickListener{ private Context mContext; private Button showButton,cancelButton; private Notification mNotification; private NotificationManager mNotificationManager; private final static int NOTIFICATION_ID = 0x0001; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } //这里是初始化一些操作,可以看到onCreate()方法里代码非常简洁。 public void setupViews(){ mContext = NotificationDemo.this; showButton = (Button)findViewById(R.id.showButton); cancelButton = (Button)findViewById(R.id.cancelButton); mNotification = new Notification(R.drawable.icon,"This is a notification.",System.currentTimeMillis()); //将使用默认的声音来提醒用户 mNotification.defaults = Notification.DEFAULT_SOUND; mNotificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); showButton.setOnClickListener(this); cancelButton.setOnClickListener(this); } //按钮点击事件响应 public void onClick(View v) { if(v == showButton){ Intent mIntent = new Intent(mContext,NotificationDemo.class); //这里需要设置Intent.FLAG_ACTIVITY_NEW_TASK属性 mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent mContentIntent =PendingIntent.getActivity(mContext,0, mIntent, 0); //这里必需要用setLatestEventInfo(上下文,标题,内容,PendingIntent)不然会报错. mNotification.setLatestEventInfo(mContext, "10086", "您的当前话费不足,请充值.哈哈~", mContentIntent); //这里发送通知(消息ID,通知对象) mNotificationManager.notify(NOTIFICATION_ID, mNotification); }else if(v == cancelButton){ //取消只要把通知ID传过来就OK了. mNotificationManager.cancel(NOTIFICATION_ID); } }}
运行Android工程,效果如下图所示:
http://dl.iteye.com/upload/attachment/424103/4bcccaf7-9cf7-33d4-9fb8-21caeb51d371.gif
http://dl.iteye.com/upload/attachment/424106/6e6ce857-29a4-3880-89aa-537baeebaf08.gif
http://dl.iteye.com/upload/attachment/424105/94b80ebb-1d9f-3053-b982-02427a993a1e.gif
http://dl.iteye.com/upload/attachment/424109/43cfe7a1-5275-30c8-ab9a-ab951463173b.gif
页:
[1]