付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中怎么利用Notification实现在状态栏上显示通知
本篇文章为大家展示了Android中怎么利用Notification实现在状态栏上显示通知,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。(1)调用getSystemService()方法获取系统的NotificationManager服务。(2)创建一个Notification对象,并为其设置各种属性(3)为Notification对象设置事件信息(4)通过NotificationManager类的notify()方法发送Notification通知下面通过一个具体的实例说明如何使用Notification在状态栏上显示通知:res/layout/main.xml:<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout1"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示通知"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除通知"/>
</LinearLayout>这个是点击通知跳转的页面main2.xml:<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是详细内容"/>
</LinearLayout>在中AndroidManifest.xml添加一下两个权限,并在<application>标签中注册ContentActivity:<!--
添加操作闪光灯的权限
-->
<uses-permission
android:name="android.permission.FLASHLIGHT"/>
<!--
添加操作震动器的权限
-->
<uses-permission
android:name="android.permission.VIBRATE"/>
<application>
<activity
android:name=".ContentActivity"/>
</application>MainActivity:package
com.example.test;
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.view.View.OnClickListener;
import
android.widget.Button;
public
class
MainActivity
extends
Activity
{
public
static
int
NOTIFYID_1=1,NOTIFYID_2=2;
@Override
public
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取通知管理器,用于发送通知
final
NotificationManager
notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Button
button1=(Button)
findViewById(R.id.button1);//获取"显示通知"按钮
//为"显示通知"按钮添加单击事件监听器
button1.setOnClickListener(new
OnClickListener()
{
@Override
public
void
onClick(View
arg0)
{
Notification
notify=new
Notification();//创建一个Notification对象
notify.icon=R.drawable.in;
notify.tickerText="显示第一个通知";
notify.when=System.currentTimeMillis();//设置发送时间(设置为当前时间)
notify.defaults=Notification.DEFAULT_ALL;//设置默认声音、默认震动和默认闪光灯
notify.setLatestEventInfo(MainActivity.this,
"无题",
"每天进步一点点",
null);//设置事件信息
notificationManager.notify(NOTIFYID_1,notify);//通过通知管理器发送通知
//添加第二个通知
Notification
notify1=new
Notification(R.drawable.music,"显示第二个通知",System.currentTimeMillis());
notify1.flags=Notification.FLAG_AUTO_CANCEL;//打开应用程序后图标消失
Intent
intent=new
Intent(MainActivity.this,ContentActivity.class);//设置为跳转页面准备的Intent
//针对意图的包装对象,在下面就是通知被点击时激活的组件对象(上下文,请求码,意图对象,标识符)
PendingIntent
pendingIntent=PendingIntent.getActivity(MainActivity.this,
0,
intent,
0);
//设置通知的内容
(上下文对象,标题,
内容,
指定通知被点击的时候跳转到哪里,激活哪个组件)
notify1.setLatestEventInfo(MainActivity.this,
"通知",
"查看详细内容",
pendingIntent);
notificationManager.notify(NOTIFYID_2,notify);//通过通知管理器发送通知
}
});
Button
button2=(Button)
findViewById(R.id.button2);//获取"删除通知"按钮
//为"显示通知"按钮添加单击事件监听器
button2.setOnClickListener(new
OnClickListener()
{
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年新大纲解读与配套习题
- 2026年解析宿州市中石化岗位晋升途径
- 2026年国企科技创新面试题库
- 2026年村庄清洁行动长效保洁机制知识竞赛
- 2026年机关新人借调期间纪律要求问答
- 2026年粮食应急保障网点供应加工知识测试
- 2026年公务员面试仿真题精解与答题误区纠正
- 2026年中石油宜宾分公司数智化岗面试常见问答集
- 2026年季度业绩反馈回复函(8篇范文)
- 供应商欠款催收通知(6篇)范文
- 《分析人类活动对生态环境的影响》生物教学课件
- 2026年体育教师招聘考试真题及答案
- 义务教育均衡发展质量监测八年级综合试卷(附答案)
- 宠物美容师就业合同协议(2025年工作规范)
- 基因治疗产品生产工艺清洁验证残留限度
- 2025年吐鲁番市法检系统招聘聘用制书记员考试(23人)模拟试卷及参考答案
- 三年(2023-2025)广东中考化学真题分类汇编:专题09 质量守恒定律和化学方程式(原卷版)
- 金属非金属矿山安全培训管理规定
- DB53-T 1188-2023 植保无人飞机防治烟草病虫害作业技术规程
- 兴奋剂药品知识培训课件
- 新版中华民族共同体概论课件第十二讲民族危亡与中华民族意识觉醒(1840-1919)-2025年版
评论
0/150
提交评论