Android编程使用Service实现Notification定时发送功能示例
作者:迟做总比不做强 发布时间:2023-03-12 09:54:44
标签:Android,Service,Notification
本文实例讲述了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程序设计有所帮助。
来源:http://blog.csdn.net/true100/article/details/51279395


猜你喜欢
- 前言现在APP中用到H5页面的越来越多,而如何正确获取WebView的网页title是必须要考虑的。最近做项目的时候,老大让我把之前做的we
- Process类的作用是对系统进程进行管理,我们使用Process类中的一些方法结合Winform开发个简单的进程管理器: 在使用Proce
- 目录安装 BenchmarkDotNet什么是基准测试创建基准测试代码运行 benchmarkBenchmarkDotNet 是一个轻量级,
- 1.for循环import com.google.common.base.Function;import com.google.common
- 日志是非常重要的,虽然他不会以需求功能提来,但也不会体现在产品方案中。但是,它在系统项目中却占有巨大的地位。为了保证服务的高可用,发现问题一
- 错误使用New HttpClient如下面一段代码,日常开发中经常使用的 call http 方式,每次 new 一个 HttpClient
- 前后端分离的项目,前端有菜单(menu),后端有API(backendApi),一个menu对应的页面有N个API接口来支持,本文介绍如何基
- /** * 进行BigDecimal对象的加减乘除,四舍五入等运算的工具类 * * @author Marydon * @createTi
- java volatile关键字在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多
- Java Map.values()方法获取Map集合中的所有键值对象Java 集合类中的 Map.values() 方法用来获取
- 异步客户端套接字示例 下面的示例程序创建一个连接到服务器的客户端。该客户端是用异步套接字生成的,因此在等待服务器返回
- 在【解决方案资源管理器】中找到Form1.cs,单击,快捷键F2重命名为“Login.cs”(命名很
- using System.Xml;//初始化一个xml实例XmlDocument xml=new XmlDocument();//导入指定x
- 泛型的概述和优势泛型概述泛型:是JDK5中引入的特性,可以在编译阶段约束操作的数据类型,并进行检查。泛型的格式:<数据类型>;
- 今天给大家提供一个由今天给大家提供一个由Java swing实现的酒店管理系统,数据库采用sqlserver,我会贴上部分代码,完整的代码请
- 异常算术异常类:ArithmeticExecption空指针异常类:NullPointerException类型强制转换异常:ClassCa
- 前言最近因为同事bean配置的问题导致生产环境往错误的redis实例写入大量的数据,差点搞挂redis。经过快速的问题定位,发现是同事新增一
- 1 前言项目中,目前主流的当然是微服务项目。为了应对高并发,以及保证自己的服务比较稳定,通常会把服务按照模块,或者具体的业务划分为多个独立的
- 本文实例讲述了Android开发之组件GridView简单使用方法。分享给大家供大家参考,具体如下:案例:简单的图片浏览器,保存图片到相册保
- using System;using System.Collections.Generic;using System.Linq;using