Notification和NotificationManager的使用(一)

 使用Notifaction和NotifactionManager的目的

目的:Broadcast Receiver组件并没有提供可视化界面来显示广播信息,而Notifaction和NotifactionManager可以实现可视化的信息显示,通过它们可以显示广播信息的内容以及图标和震动等信息(在状态栏上)

具体步骤:

1)获得系统级的服务Notification Manager,调用getSystemService()方法实现

  String service = NOTIFICATION_SERVICE;
  nm=(NotificationManager)getSystemService(service);

2)然后实例化Notification,设置其属性(要显示的),

 //实例化Notification
        n = new Notification();
        //设置显示图标,该图标会在状态状态栏显示
        int icon = n.icon=R.drawable.notification;
        //设置显示提示信息,该信息会在状态栏显示
        String tickerText="Test Notification";
        //显示时间
        long when = System.currentTimeMillis();
        n.icon=icon;
        n.tickerText=tickerText;
        n.when=when;


当然也可以通过

Notification n1 = new Notification(icon,tickerText,when);

此外,设置视图中图标和时间可以通过:

Intent intent = new Intent(MainActivity.this,MainActivity.class);

3)通过Notification Manager发出通知

//实例化intent
				Intent intent = new Intent(MainActivity.this,MainActivity.class);
				//获取pendingIntent
				PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
				//设置事件信息
				n.setLatestEventInfo(MainActivity.this, "My Title", "My Content", pi);
				//发出通知
				nm.notify(ID,n);


 

具体的代码,MainActivity,java

package com.ex;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button btn_send,btn_cancel;
	//声明Notifaction
	private Notification n;
	//声明NotificationManager
	private NotificationManager nm;
	//Notification标识ID
	private static final int ID = 1;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //实例化两个按钮
        btn_send=(Button)findViewById(R.id.btnsend);
        btn_cancel=(Button)findViewById(R.id.btncancel);
        //获得NotifactionManager实例
        String service = NOTIFICATION_SERVICE;
        nm=(NotificationManager)getSystemService(service);
        
        //实例化Notification
        n = new Notification();
        //设置显示图标,该图标会在状态状态栏显示
        int icon = n.icon=R.drawable.notification;
        //设置显示提示信息,该信息会在状态栏显示
        String tickerText="Test Notification";
        //显示时间
        long when = System.currentTimeMillis();
        n.icon=icon;
        n.tickerText=tickerText;
        n.when=when;
        //为图标添加监听器
        btn_send.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				//实例化intent
				Intent intent = new Intent(MainActivity.this,MainActivity.class);
				//获取pendingIntent
				PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
				//设置事件信息
				n.setLatestEventInfo(MainActivity.this, "My Title", "My Content", pi);
				//发出通知
				nm.notify(ID,n);
				
			}});
        btn_cancel.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				nm.cancel(ID);
				
			}});
    }
}


main.xml




 
 


效果:

点击发出通知后状态栏的显示如

取消通知按钮

你可能感兴趣的:(Android,Start)