码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发系列(二十四):Notification的功能与使用方法

时间:2017-06-04 13:45:28      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:layout   encoding   应用   test   rate   声音   draw   权限   new   

关于消息的提示有两种:一种是Toast,一种就是Notification。前者维持的时间比較短暂,后者维持的时间比較长。

并且我们寻常手机的应用比方网易、贴吧等等都有非常多的推送消息。就是用Notification实现的。


Notification是显示在手机状态栏的通知—手机状态栏位于手机屏幕的上方。程序一般通过NotificationManager服务来发送Notification通知

Notification的一些方法。接下来我们都可以用到:

setDefaults():设置通知LED等、音乐、震动等等。

setAutoCancel():设置点击通知后。状态栏自己主动删除通知。

setContentTitle():设置通知的标题

setContentText():设置通知的内容

setTicker():设置通知的提示信息

setSmallIcon():为通知设置图标(注意这种方法第三个是i的大写。不是L的小写)


发送Notification的步骤:

1、调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的Notification Manager服务

2、通过构造器创建一个Notification对象。

3、为Notification设置各种属性。

4、通过NotificationManager发送Notification。


在这里,我们要注意一点要在AndroidManifest.xml文件里加入几个权限:

<!-- 加入操作闪光灯的权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT" />
<!-- 加入操作振动器的权限 -->
<uses-permission android:name="android.permission.VIBRATE" />



接下来,我们通过详细的代码来说明。

main.xml:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送Notification" android:onClick="send" /> </LinearLayout> </span>

这里设置了一个button。点击会发送通知


然后。我们看下NotificationTest.java的代码:

<span style="font-size:14px;">package cn.notificationtest.com;

import cn.notificationtest.com.R;

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
{
	static final int NOTIFICATION_ID = 0x123;
	NotificationManager nm;

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

	// 为发送通知的button的点击事件定义事件处理方法
	public void send(View source)
	{
		// 创建一个启动其它Activity的Intent
		Intent intent = new Intent(NotificationTest.this
			, OtherActivity.class);
		PendingIntent pi = PendingIntent.getActivity(
			NotificationTest.this, 0, intent, 0);
		Notification notify = new Notification.Builder(this)
			// 设置打开该通知。该通知自己主动消失
			.setAutoCancel(true)
			// 设置显示在状态栏的通知提示信息
			.setTicker("网易新闻")
			// 设置通知的图标
			.setSmallIcon(R.drawable.notify)
			// 设置通知内容的标题
			.setContentTitle("这是新闻标题")
			// 设置通知内容
			.setContentText("这是新闻的内容:*************")
			// // 设置使用系统默认的声音、默认LED灯
			// .setDefaults(Notification.DEFAULT_SOUND
			// |Notification.DEFAULT_LIGHTS)
			// 设置通知的自己定义声音
			.setSound(Uri.parse("android.resource://cn.notificationtest.com/"+R.raw.msg))
			.setWhen(System.currentTimeMillis())
			// 设改通知将要启动程序的Intent
			.setContentIntent(pi).getNotification();
		// 发送通知
		nm.notify(NOTIFICATION_ID, notify);
	}
}</span>
在这个java文件里,我们通过构造器创建了一个Notification对象。然后为Notification设置各种属性。最后通过NotificationManager发送Notification。

(这里须要注意的一点是,我们定义的声音,图标什么的都是个人创建)


通过上边的java代码,我们创建了一个Intent对象,能够通过这条通知。切换到另外的一个Activity界面:OtherActivity

<span style="font-size:14px;">/**
 *
 */
package cn.notificationtest.com;

import cn.notificationtest.com.R;

import android.app.Activity;
import android.os.Bundle;

public class OtherActivity extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//设置该Activity显示的页面
		setContentView(R.layout.other);
	}
}
</span>


效果图例如以下所看到的:

技术分享




Android开发系列(二十四):Notification的功能与使用方法

标签:layout   encoding   应用   test   rate   声音   draw   权限   new   

原文地址:http://www.cnblogs.com/cynchanpin/p/6940126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!