Notification与NotificationManager通知功能应用

来源:互联网 发布:公安情报大数据 编辑:程序博客网 时间:2024/05/02 00:22

           Notification是android系统中的一种通知服务,通过状态栏、手机震动、LED和提示音等多种通知方式提供了良好的用户体验。

           Notification使用步骤如下:

            1)获取NotificationManager对象。通过调用系统NOTIFICATION_SERVICE服务,获取NotificationManager实例,代码如下:

                  NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

            2) 创建Notification对象

            3) 设置Notification对象的各项属性,以下提供各项属性:

                 audioStreamType:设置档声音响起时,所用的音频流的类型

                 contentIntent:设置当通知条目被单击,所执行的Intent

                 contentView:设置通知被显示在状态条上时,所显示的视图

                 defaults:设置默认值

                 deleteIntent:设置单击状态条上的“清除”按钮时,所执行定的Intent

                 icon:设置状态条所用的图片

                 iconLevel:设置状态条中多个图片的级别

                 ledAREG:设置LED灯的颜色

                 ledOffMS:设置LED灯关闭时的闪光时间(毫秒为单位)

                 ledOnMS:设置LED灯开始时的闪光时间(毫秒为单位)

                 number:设置这个通知代表事件的号码 

                 sound:设置通知的声音

                 tickerText:设置通知在状态中显示的信息

                 vibrate:设置震动模式

                 when:设置通知的时间戳       

            4) 执行这个Notification通知,使用notificationManager.notify(id,notification)完成通知的发送操作。

             

         不多说了,直接上代码NotificationDemoActivity.java

         

package com.android;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.media.RingtoneManager;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class NotificationDemoActivity extends Activity {    private Button txtBtn;private Button soundBtn;private Button vibrateBtn;private Button ledBtn;private Button closeBtn;private  NotificationManager nm;public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);                txtBtn = (Button)findViewById(R.id.textN);        txtBtn.setOnClickListener(new OnClickListener(){public void onClick(View v) {Notification not = new Notification();not.icon = R.drawable.ic_launcher;//打开通知后显示的图片not.tickerText = "this is text notification";//弹出的提示信息PendingIntent intent = PendingIntent.getActivity(NotificationDemoActivity.this, 0, new Intent(NotificationDemoActivity.this,NotificationDemoActivity.class), 0);not.setLatestEventInfo(NotificationDemoActivity.this, "nofification", "Content of Notification Demo", intent);//打开通知后显示的内容nm.notify(0, not);}});                        soundBtn = (Button)findViewById(R.id.soundN);        soundBtn.setOnClickListener(new OnClickListener(){public void onClick(View v) {Notification not = new Notification();Uri ring = RingtoneManager.getActualDefaultRingtoneUri(NotificationDemoActivity.this, RingtoneManager.TYPE_RINGTONE);//获取系统当前铃声not.sound = ring;nm.notify(0, not);}});                        vibrateBtn = (Button)findViewById(R.id.vibrateN);        vibrateBtn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubNotification not = new Notification();not.vibrate = new long[]{0,100,200,300};nm.notify(0, not);//}});                        ledBtn = (Button)findViewById(R.id.ledN);        ledBtn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {Notification not = new Notification();not.ledOnMS = 300;//设置开始时的闪光时间not.ledOffMS = 1000;//设置关闭时的闪光时间nm.notify(0, not);}});                        closeBtn = (Button)findViewById(R.id.close);        closeBtn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {nm.cancel(0);}});    }}

           因为使用了震动功能,需要在清单文件中注册振动器权限"android.permission.VIBRATE".

          清单文件Androidmanifest.xml

          

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.android"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="10" />    <uses-permission android:name="android.permission.VIBRATE"/>    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:label="@string/app_name"            android:name=".NotificationDemoActivity" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
              布局仅5个按钮,比较简单,直接上图。