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

来源:互联网 发布:淘宝怎么借贷宝 编辑:程序博客网 时间:2024/05/01 00:23

现在的很多应用中都会存在通知的使用,例如,新消息的提醒、广告推送、新闻消息等。下面通过一个实例来熟悉一下通知的使用。

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的idprivate Button button1;private Button button2;@Overrideprotected 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);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (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"/>

源码下载

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

详细内容参考地址:

http://my.oschina.net/wangjunhe/blog/113855

http://blog.csdn.net/vipzjyno1/article/details/25248021

http://www.bkjia.com/Androidjc/924831.html

0 0