Android[初级教程]第十二章 Notification的应用

本文通过西游记的比喻介绍了Notification的概念,并详细讲解了如何在Android中创建和发送Notification,包括创建NotificationManager,设置通知图标、消息、声音、震动等属性,以及如何响应用户的操作来取消通知。最后,文章提供了AndroidManifest.xml文件的修改示例和最终运行效果的展示。

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

这一章节,我们来学习Notification的应用,很多人问Notification是什么东东啊?我打个比方吧,还是以西游记来说:唐僧被妖怪们抓住了,那悟空得知道是哪个妖怪抓住了他师傅,他得变成一些动物(苍蝇或蚊子)去通知他师傅啊,通知唐僧悟空来救他了,这里通知就是Notification,那唐僧知道了,他就放心了,安心等着悟空来救.呵呵,让我看一下main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="通知师傅悟空来救他了" android:id="@+id/wukong" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="通知师傅八戒来救他了" android:id="@+id/bajie" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="通知师傅沙僧来救他了" android:id="@+id/shaseng" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="取消通知" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/notify_cancal"/> </LinearLayout>


这里面没有什么特别的,只是定义了四个按钮,然后主Activity.java

import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class NotificationDemo extends Activity { private final static int NOTIFYCATION_ID = 0x11; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.notification); //查找main.xml中的Button控件 Button wukong = (Button) findViewById(R.id.wukong); wukong.setOnClickListener(new ClickEvent()); Button bajie = (Button) findViewById(R.id.bajie); bajie.setOnClickListener(new ClickEvent()); Button shaseng = (Button) findViewById(R.id.shaseng); shaseng.setOnClickListener(new ClickEvent()); Button notify_cancal = (Button)findViewById(R.id.notify_cancal); notify_cancal.setOnClickListener(new ClickEvent()); } class ClickEvent implements View.OnClickListener { @Override public void onClick(View v) { //获取系统的NotificationManager服务 NotificationManager notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //创建一个启动其他Activity的Intent Intent intent = new Intent(NotificationDemo.this, otherActivity.class); //设置点击通知时显示内容启动的类 PendingIntent pi = PendingIntent.getActivity(NotificationDemo.this, 0, intent, 0); //创建Notification对象 Notification notify = new Notification(); //设置Notification的发送时间 notify.when = System.currentTimeMillis(); //为Notification设置默认的声音,默认的振动,默认的闪光灯,但这些需要在AndroidManifest.xml中加入相应的权限,不然会报错 notify.defaults = Notification.DEFAULT_ALL; switch (v.getId()) { case R.id.wukong: //设置Notification的图标 notify.icon = R.drawable.wukong; //设置Notification事件信息 notify.setLatestEventInfo(NotificationDemo.this, "悟空", "悟空来救师傅的通知", pi); //设置Notification的文本内容,会显示在状态栏中 notify.tickerText = "悟空来救师傅了"; //发送通知 notifyManager.notify(NOTIFYCATION_ID, notify); break; case R.id.bajie: //同上 notify.icon = R.drawable.bajie; notify.setLatestEventInfo(NotificationDemo.this, "八戒", "八戒来救师傅的通知", pi); notify.tickerText = "八戒来救师傅了"; notifyManager.notify(NOTIFYCATION_ID, notify); break; case R.id.shaseng: //同上 notify.icon = R.drawable.shaseng; notify.setLatestEventInfo(NotificationDemo.this, "沙僧", "沙僧来救师傅的通知", pi); notify.tickerText = "沙僧来救师傅了"; notifyManager.notify(NOTIFYCATION_ID, notify); break; case R.id.notify_cancal: //取消通知 notifyManager.cancel(NOTIFYCATION_ID); } } } }

OK,我们在AndroidManifest.xml中加入相应的权限和要启动的Activity

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kang.button_demo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission> <uses-permission android:name="android.permission.VIBRATE"></uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:label="@string/app_name" android:name=".NotificationDemo"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".otherActivity"></activity> </application> </manifest>

好了,都写完了,运行一下呢,如图所示:

这里的要点就是Notification和NotificationManager的创建,其实掌握Notification对于Android应用开发还是很有用的,好了,这一章就结束了,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值