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

Android中通知(Notification)的简单使用

2014-10-11 15:00 120 查看
现在的很多应用中都会存在通知的使用,例如,新消息的提醒、广告推送、新闻消息等。下面通过一个实例来熟悉一下通知的使用。

1、布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="显示通知" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:text="取消通知" />

</RelativeLayout>
2、通知使用实现

package com.cx.notificatest;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
	private NotificationManager manager;
	private int notification_id; //对应的Notification的id
	private Button button1;
	private Button button2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		
		//通知控制这类,用于显示通知,NotificationManager是系统常用服务
		manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			sendNotification();
			break;

		case R.id.button2:
			manager.cancel(notification_id);
			break;
		}
	}
	
	/**
	 * 构造Notification并发送到通知栏
	 */
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	private void sendNotification(){
		//用于点击跳转 ,点击通知跳转到本页
		Intent openintent = new Intent(this, MainActivity.class);  
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, openintent, 0);  
		
		Builder builder = new Notification.Builder(this);
		builder.setSmallIcon(R.drawable.ic_launcher);//设置图标
		builder.setTicker("新消息");//手机状态栏提示
		builder.setWhen(System.currentTimeMillis());//设置时间
		builder.setContentTitle("通知标题");//设置标题
		builder.setContentText("通知内容");//设置通知内容
		builder.setContentIntent(pendingIntent);//设置点击后的意图
//		builder.setDefaults(Notification.DEFAULT_SOUND);//设置提示声音
		builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置提示指示灯,需要权限
		builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动,需要权限
//		builder.setDefaults(Notification.DEFAULT_ALL);//上面三个全设置
		
		builder.setOnlyAlertOnce(true);//更新信息不再重新弹出  
		builder.setAutoCancel(true);//单击后自动取消 
		builder.setProgress(100, 50, false);//进度条  
		builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.xxx));//自定义提示音
		
		Notification notification = builder.build();//Android4.1以上使用
//		Notification notification = builder.getNotification();//Android4.1以下使用
		
		notification.icon = R.drawable.notice; //设置弹出图片
		notification.flags = Notification.FLAG_NO_CLEAR; //设置不可取消
		/**
		 * 同时上面的某些属性也可以在此处设置
		 */
//		notification.defaults=Notification.DEFAULT_SOUND; //添加系统声音
//		notification.sound=Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.xxx);//添加自定义声音
		
		//显示通知
		manager.notify(notification_id, notification);
	}
}
3、添加指示灯和震动权限

<!-- 指示灯权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!-- 震动权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>


源码下载

使用上面代码就基本能够实现的通知要求,如果你的需求更加的繁琐,可以去看看下面的链接。

详细内容参考地址:

/article/3516899.html
http://blog.csdn.net/vipzjyno1/article/details/25248021 http://www.bkjia.com/Androidjc/924831.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: