Notification的功能与用法

本文详细介绍了如何在Android应用中使用NotificationManager发送和取消通知,包括创建Notification对象、设置各种属性及权限声明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * 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. */
	@Override
	public 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() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 创建一个启动其他activity的Intent
				Intent intent = new Intent(Ex002_13Activity.this,
						otherActivity.class);
				PendingIntent pi = PendingIntent.getActivity(
						Ex002_13Activity.this, 0, intent, 0);
				// 创建一个Notification
				Notification 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() {
			
			@Override
			public 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" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值