NotificationManager

来自个人维基
跳转至: 导航搜索

Notification

Android提供了Notification功能用于通知事件。 最常见的就是StatusBar上的使用。


使用例子

        //取得服务管理器
	private NotificationManager mNotyMgr;
         mNotyMgr = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
 
        //调用这个函数来发出notify
        //id为一个icon图标,text为第一次上bar通知时候提示的信息
         //下拉列表中的通知在myNotify.setLatestEventInfo函数中设置
         //通知的唯一性由NotificationManager.notify()的第一个参数确定,本例为notifyIndex
        //DnNotifyActivity为上bar下拉选中notify时启动的activity
	public void setNotify(int id,String text)
	{
		Intent notifyIntent = new Intent(this,DnNotifyActivity.class);
		notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 
		PendingIntent appIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
 
		Notification myNotify = new Notification();
		myNotify.icon = id;
		myNotify.tickerText = text;
		myNotify.defaults = Notification.DEFAULT_SOUND;
 
		myNotify.setLatestEventInfo(this, "Notify通知", "Notify按钮被按下了", appIntent);
		mNotyMgr.notify(notifyIndex,myNotify);
		notifyIndex++;
	}
 
 
//DnNotifyActivity源码
package com.ydn;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class DnNotifyActivity extends Activity implements OnClickListener
{
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
 
        Toast.makeText(this, "这是在Notify调用的Activity中显示的Toast", Toast.LENGTH_SHORT).show();
    }
 
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		setTitle("backup button clicked!");
		finish();
	}
 
}