Notification的功能与用法

来源:互联网 发布:socket编程视频教程 编辑:程序博客网 时间:2024/05/19 22:48
/*
 * Notification的功能与用法
 * Notification是显示在手机状态栏的消息
 * ----手机状态栏位于手机屏幕的最上方,Notification
 * 是具有全局效果的通知,程序一般是通过NotificationManager
 * 服务来发送Notification
 *
 * 使用Notification发送Notification的步骤如下:
 * 1.调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的
 *   NotificationManager服务
 * 2.通过构造器构造一个Notification对象
 * 3.为Notification设置各种属性
 * 4.通过NotificationManager发送Notification
 */
import 略
public class Ex002_13Activity extends Activity {static final int NOTIFICATION_ID = 0x1123;private Button send, cancel;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 获取应用界面中的Button对象send = (Button) findViewById(R.id.send);cancel = (Button) findViewById(R.id.cancel);// 为按钮添加监听send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 创建一个启动其他activity的IntentIntent intent = new Intent(Ex002_13Activity.this,otherActivity.class);PendingIntent pi = PendingIntent.getActivity(Ex002_13Activity.this, 0, intent, 0);// 创建一个NotificationNotification notify = new Notification();// 为Notification设置图标,该图标显示在状态栏中notify.icon = R.drawable.notify;// 为Notification设置文本内容notify.tickerText = "你有一条新短信";// 为Notification设置发送时间notify.when = System.currentTimeMillis();// 为Notification设置声音notify.defaults = Notification.DEFAULT_SOUND;// 为Notification设置默认的声音、默认的震动、默认的闪光灯notify.defaults = Notification.DEFAULT_ALL;// 设置事件信息notify.setLatestEventInfo(Ex002_13Activity.this, "普通通知","点击查看", pi);// 获取系统的NotificationManager服务NotificationManager notifyM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// 发送通知notifyM.notify(NOTIFICATION_ID, notify);}});cancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 获取系统的NotificationManager服务NotificationManager notifyM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//取消通知notifyM.cancel(NOTIFICATION_ID);}});}}
xml布局文件很简单:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <Button        android:id="@+id/send"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/send" />    <Button        android:id="@+id/cancel"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/cancel" /></LinearLayout>

下面我们来看下运行后的结果:

总结:程序是统统default属性为Notification设置了各种属性,如果不想用默认的则可以如下设置:
//设置声音
notify.sound=Uri.parse("你的音乐存放路径");
//设置自定义震动
notify.vibrate=new long[]{0,50,100,150}
//设置闪光灯的颜色
notify.ledARGB=你要的颜色,比如红色:0xffff0000
//设置闪光灯多少毫秒后熄灭
notify.ledOffMS=你设置的时间,以毫秒为单位
//设置闪光灯多少毫秒后开启
notify.ledOnMS=你设置的时间,以毫秒为单位

代码中还关联了一个Activity:otherActivity,因此我们需要在AndroidManifest.xml文件中声明该Activity。
程序中还涉及访问系统的闪光灯、震动这也需要在AndroidManifest.xml声明权限,声明代码如下:
 <!-- 添加操作闪光灯的权限 -->
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <!-- 添加操作震动器的权限 -->
    <uses-permission android:name="android.permission.VIBRATE" />