Notification与NotificationManager通知功能应用

本文详细介绍了Android应用中的通知系统,包括如何获取NotificationManager对象、创建和配置Notification对象,以及发送通知的过程,并提供了示例代码帮助开发者更好地理解和实现通知功能。

           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(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Notification 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(){

			@Override
			public 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(){
			@Override
			public 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个按钮,比较简单,直接上图。

                      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值