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

Android手机状态栏通知(Notification)的功能与用法

2015-02-13 21:28 465 查看

1、Notification 是显示在手机状态栏的通知——手机状态栏位于手机屏幕的最上方那里一般显示了手机当前网络状态、电池状态、时间等。

2、Notification 代表的是一种具有全局效果的通知,程序一般通过NotificationManager 服务来发送Notification。

3、NotificationManager 是一个重要的系统服务,位于应用程序框架层,应用程序可通过NotificationManager 向系统发送全局通知。

4、通过Notification.Builder 类允许开发者更轻松地创建Notification对象。

5、Notification.Builder 提供的常用方法如下:

①setDefaults() ===> 设置通知LED 灯、音乐、震动等

 该方法支持的属性值:

 DEFAULT_SOUND:设置使用默认声音

 DEFAULT_VIBRATE:设置使用默认震动

 DEFAULT_LIGHTS:设置使用默认闪光灯

 ALL:设置使用默认声音、震动、闪光灯

②setAutoCancel() ===> 设置点击通知后,状态栏自动删除通知

③setContentTitle() ===> 设置通知标题

④setContentText() ===> 设置通知内容

⑤setSmalllcon() ===> 为通知设置图标

⑥setLargelcon() ===> 为通知设置大图标

⑦setTick() ===> 设置通知在状态栏的提示文本

⑧setContentlntent() ===> 设置点击通知后将要启动的程序组件对应的Pendinglntent

6、发送Notification 的步骤:

①调用getSystemService(NOTIFICATION_SERVICE) 方法获取系统的NotificationManager 服务

②通过构造器创建一个Notification 对象

③为Notification 设置各种属性

④通过NotificationManager 发送Notification 的功能和用法

7、访问系统功能须加权限

 <!-- 添加操作闪光灯的权限 -->

 <uses-permission android:name="android.permission.FLASHLIGHT" />

 <!-- 添加操作振动器的权限 -->

 <uses-permission android:name="android.permission.VIBRATE" /> 

 

8、要启动另一个页面,别忘了在AndroidManifest.xml 文件中声明该Activity

 <activity

  android:name=".OtherActivity"

  android:label="@string/other_activity">

 </activity> 

9、具体看如下代码

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="org.crazyit.ui"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />
<!-- 添加操作闪光灯的权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT" />
<!-- 添加操作振动器的权限 -->
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".NotificationTest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".OtherActivity"
android:label="@string/other_activity">
</activity>
</application>
</manifest>
package org.crazyit.ui;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class NotificationTest extends Activity {
private static final int NOTIFICATION_ID = 0x123;
private NotificationManager nm;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取系统的NotificationManager服务
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}

// 为发送通知的按钮的点击事件定义事件处理方法
public void send(View source) {
// 创建一个启动其他Activity的Intent
Intent intent = new Intent(NotificationTest.this, OtherActivity.class);
// 单击Notification 通知时将会启动Intent 对应的程序,实现页面的跳转
PendingIntent pi = PendingIntent.getActivity(NotificationTest.this, 0, intent, 0);

Notification notify = new Notification.Builder(this)
// 设置打开该通知,该通知自动消失
.setAutoCancel(true)
// 设置显示在状态栏的通知提示信息
.setTicker("有新消息")
// 设置通知的图标
.setSmallIcon(R.drawable.notify)
// 设置通知内容的标题
.setContentTitle("一条新通知")
// 设置通知内容
.setContentText("恭喜你,您加薪了,工资增加20%!")
// // 设置使用系统默认的声音、默认LED灯
// .setDefaults(Notification.DEFAULT_SOUND
// |Notification.DEFAULT_LIGHTS)
// 设置通知的自定义声音
.setSound(
Uri.parse("android.resource://org.crazyit.ui/"
+ R.raw.msg))
.setWhen(System.currentTimeMillis())
// 设改通知将要启动程序的Intent
.setContentIntent(pi)
.getNotification();
// 发送通知
nm.notify(NOTIFICATION_ID, notify);
}

// 为删除通知的按钮的点击事件定义事件处理方法
public void del(View v) {
// 取消通知
nm.cancel(NOTIFICATION_ID);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息