您的位置:首页 > 移动开发 > Android开发

Android入门进阶教程(21)-通知管理器NotificationManager

2013-06-04 11:46 399 查看
通知:Notification 

通知管理器:NotificationManager 

1、使用Notification 和 NotificationManager的目的: 

* Broadcast Receiver没有提供可视化界面来显示广播信息; 

* Notification 和 NotificationManager 能实现可视化信息的显示; 

* 可以将显示的广播信息的内容以及图标和震动等信息(在状态栏上); 

2、使用小贴士: 

* getSystemService();     获取系统级的服务; 

* 实例化Notification; 对属性设置:icon tickerText when -- 发出一些通知的属性; 

* n.setLatestEventInfo();设置事件信息 

* 通过NotificationManager发出通知;nm.notify(); 

3、案例:用户点击按钮发出一个通知,同样有一个取消通知的按钮;
 

Java代码  


package com.example.nofifacation;  

  

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 final int ID = 1;  

    /* 声明Notification 和 NotificationManager */  

    private Notification n ;  

    private NotificationManager nm;  

  

    private Button send;  

    private Button cancel;  

    /* 组件初始化 */  

    public void init(){  

        send = (Button) findViewById(R.id.sendNotification);  

        cancel = (Button) findViewById(R.id.cancelNotification);  

    }  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.main);  

          

        init();  

          

        /* 获取Notification对象 *//* 获得NotificationManager对象 */  

        n = new Notification();  

        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  

        /* 设置显示图标,该图标会在状态栏显示 */  

        n.icon = R.drawable.ic_launcher;  

          

        /* 设置显示提示信息,也会在状态栏显示 */  

        n.tickerText = "通知,测试通知的发出";  

          

        /* 显示时间 */  

        n.when = System.currentTimeMillis();  

          

        /* 按钮点击事件监听器 */  

        send.setOnClickListener(new OnClickListener() {  

  

            public void onClick(View v) {  

                  

                /* 实例化Intent对象 *//* 在同一个Activity之间跳转 */  

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

                  

                /* 获取PendingIntent 对象 */  

                PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);  

                  

                /* 设置事件信息 *//*  */  

                n.setLatestEventInfo(MainActivity.this, "Title", "content",pi);  

                  

                /* 发出通知 *//* 参1:通知对象ID , 通知Notification的对象 */  

                nm.notify(ID,n);  

            }  

        });  

          

        /* 点击事件监听器 *//* 动作:取消通知 */  

        cancel.setOnClickListener(new OnClickListener() {  

              

            public void onClick(View v) {  

                nm.cancel(ID);    

            }  

        });  

    }  

  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android