Android编程使用Service实现Notification定时发送功能示例
发布时间 - 2026-01-11 02:52:13 点击率:次本文实例讲述了Android编程使用Service实现Notification定时发送功能。分享给大家供大家参考,具体如下:
/**
* 通过启动或停止服务来管理通知功能
*
* @description:
* @author ldm
* @date 2016-4-29 上午9:15:15
*/
public class NotifyControlActivity extends Activity {
private Button notifyStart;// 启动通知服务
private Button notifyStop;// 停止通知服务
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notifying_controller);
initWidgets();
}
private void initWidgets() {
notifyStart = (Button) findViewById(R.id.notifyStart);
notifyStart.setOnClickListener(mStartListener);
notifyStop = (Button) findViewById(R.id.notifyStop);
notifyStop.setOnClickListener(mStopListener);
}
private OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
// 启动Notification对应Service
startService(new Intent(NotifyControlActivity.this,
NotifyingService.class));
}
};
private OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
// 停止Notification对应Service
stopService(new Intent(NotifyControlActivity.this,
NotifyingService.class));
}
};
}
/**
* 实现每5秒发一条状态栏通知的Service
*
* @description:
* @author ldm
* @date 2016-4-29 上午9:16:20
*/
public class NotifyingService extends Service {
// 状态栏通知的管理类对象,负责发通知、清楚通知等
private NotificationManager mNM;
// 使用Layout文件的对应ID来作为通知的唯一识别
private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;
/**
* Android给我们提供ConditionVariable类,用于线程同步。提供了三个方法block()、open()、close()。 void
* block() 阻塞当前线程,直到条件为open 。 void block(long timeout)阻塞当前线程,直到条件为open或超时
* void open()释放所有阻塞的线程 void close() 将条件重置为close。
*/
private ConditionVariable mCondition;
@Override
public void onCreate() {
// 状态栏通知的管理类对象,负责发通知、清楚通知等
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 启动一个新个线程执行任务,因Service也是运行在主线程,不能用来执行耗时操作
Thread notifyingThread = new Thread(null, mTask, "NotifyingService");
mCondition = new ConditionVariable(false);
notifyingThread.start();
}
@Override
public void onDestroy() {
// 取消通知功能
mNM.cancel(MOOD_NOTIFICATIONS);
// 停止线程进一步生成通知
mCondition.open();
}
/**
* 生成通知的线程任务
*/
private Runnable mTask = new Runnable() {
public void run() {
for (int i = 0; i < 4; ++i) {
// 生成带stat_happy及status_bar_notifications_happy_message内容的通知
showNotification(R.drawable.stat_happy,
R.string.status_bar_notifications_happy_message);
if (mCondition.block(5 * 1000))
break;
// 生成带stat_neutral及status_bar_notifications_ok_message内容的通知
showNotification(R.drawable.stat_neutral,
R.string.status_bar_notifications_ok_message);
if (mCondition.block(5 * 1000))
break;
// 生成带stat_sad及status_bar_notifications_sad_message内容的通知
showNotification(R.drawable.stat_sad,
R.string.status_bar_notifications_sad_message);
if (mCondition.block(5 * 1000))
break;
}
// 完成通知功能,停止服务。
NotifyingService.this.stopSelf();
}
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@SuppressWarnings("deprecation")
private void showNotification(int moodId, int textId) {
// 自定义一条通知内容
CharSequence text = getText(textId);
// 当点击通知时通过PendingIntent来执行指定页面跳转或取消通知栏等消息操作
Notification notification = new Notification(moodId, null,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, NotifyControlActivity.class), 0);
// 在此处设置在nority列表里的该norifycation得显示情况。
notification.setLatestEventInfo(this,
getText(R.string.status_bar_notifications_mood_title), text,
contentIntent);
/**
* 注意,我们使用出来。incoming_message ID 通知。它可以是任何整数,但我们使用 资源id字符串相关
* 通知。它将永远是一个独特的号码在你的 应用程序。
*/
mNM.notify(MOOD_NOTIFICATIONS, notification);
}
// 这是接收来自客户端的交互的对象. See
private final IBinder mBinder = new Binder() {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
android:text="通过Service来实现对Notification的发送管理" />
<Button
android:id="@+id/notifyStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动服务" >
<requestFocus />
</Button>
<Button
android:id="@+id/notifyStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务" >
</Button>
</LinearLayout>
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android基本组件用法总结》、《Android视图View技巧总结》、《Android资源操作技巧汇总》、《Android操作json格式数据技巧总结》、《Android开发入门与进阶教程》、《Android编程之activity操作技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
# Android
# Service
# Notification
# 定时发送
# Android入门之Service的使用详解
# Android NotificationListenerService通知监听服务使用
# Android Google AutoService框架使用详解
# Android使用Service实现IPC通信的2种方式
# 说说在Android如何使用服务(Service)的方法
# Android使用Service实现简单音乐播放实例
# 浅谈Android中Service的注册方式及使用
# Android 通知使用权(NotificationListenerService)的使用
# Android Service功能使用示例代码
# 状态栏
# 管理类
# 是一个
# 进阶
# 这是
# 操作技巧
# 上午
# 相关内容
# 给我们
# 感兴趣
# 给大家
# 自定义
# 它可以
# 跳转
# 更多关于
# 来实现
# 它将
# 所述
# 程序设计
# 应用程序
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
html5的keygen标签为什么废弃_替代方案说明【解答】
Laravel如何将应用部署到生产服务器_Laravel生产环境部署流程
如何用西部建站助手快速创建专业网站?
Bootstrap整体框架之CSS12栅格系统
Laravel如何处理跨站请求伪造(CSRF)保护_Laravel表单安全机制与令牌校验
Linux后台任务运行方法_nohup与&使用技巧【技巧】
Laravel如何自定义分页视图?(Pagination示例)
家族网站制作贴纸教程视频,用豆子做粘帖画怎么制作?
专业型网站制作公司有哪些,我设计专业的,谁给推荐几个设计师兼职类的网站?
Laravel如何使用Collections进行数据处理?(实用方法示例)
HTML5空格和margin有啥区别_空格与外边距的使用场景【说明】
如何用搬瓦工VPS快速搭建个人网站?
Laravel怎么实现API接口鉴权_Laravel Sanctum令牌生成与请求验证【教程】
Laravel如何使用Facades(门面)及其工作原理_Laravel门面模式与底层机制
手机网站制作与建设方案,手机网站如何建设?
Gemini怎么用新功能实时问答_Gemini实时问答使用【步骤】
Laravel如何清理系统缓存命令_Laravel清除路由配置及视图缓存的方法【总结】
Android滚轮选择时间控件使用详解
如何在云指建站中生成FTP站点?
laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法
Laravel如何优化应用性能?(缓存和优化命令)
小米17系列还有一款新机?主打6.9英寸大直屏和旗舰级影像
微信公众帐号开发教程之图文消息全攻略
如何快速生成专业多端适配建站电话?
详解阿里云nginx服务器多站点的配置
如何获取免费开源的自助建站系统源码?
如何在万网主机上快速搭建网站?
Laravel如何获取当前登录用户信息_Laravel Auth门面使用与Session用户读取【技巧】
如何在香港服务器上快速搭建免备案网站?
Java垃圾回收器的方法和原理总结
高性能网站服务器部署指南:稳定运行与安全配置优化方案
JavaScript模板引擎Template.js使用详解
Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势
用v-html解决Vue.js渲染中html标签不被解析的问题
高端智能建站公司优选:品牌定制与SEO优化一站式服务
Laravel Debugbar怎么安装_Laravel调试工具栏配置指南
javascript事件捕获机制【深入分析IE和DOM中的事件模型】
Laravel Vite是做什么的_Laravel前端资源打包工具Vite配置与使用
Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】
Laravel Eloquent模型如何创建_Laravel ORM基础之Model创建与使用教程
Laravel如何发送邮件_Laravel Mailables构建与发送邮件的简明教程
高端企业智能建站程序:SEO优化与响应式模板定制开发
长沙做网站要多少钱,长沙国安网络怎么样?
Laravel怎么使用artisan命令缓存配置和视图
如何用5美元大硬盘VPS安全高效搭建个人网站?
Laravel怎么配置自定义表前缀_Laravel数据库迁移与Eloquent表名映射【步骤】
JS经典正则表达式笔试题汇总
非常酷的网站设计制作软件,酷培ai教育官方网站?
Laravel怎么在Controller之外的地方验证数据
百度浏览器如何管理插件 百度浏览器插件管理方法

