android 后台服务定时通知
2014-10-09 20:51
543 查看
最近有个项目的要求是在程序退出之后,任然可以每天定时发通知,我们可以想下,其实就是后台开一个服务,然后时间到了就发下通知。
1.首先我们需要用到Service类。
先上代码在慢慢解释
自定义了一个类PushService继续Service,定义了两个类来实现添加通知和取消通知
//delayTime 延迟多久执行。
//tickerText
//contentTitle 通知栏的标题
//contentText 通知栏的内容
addNotification(int delayTime,String tickerText,String contentTitle,String contentText)
//清除通知
cleanAllNotification()
====================================
Service的启动,startService来启动服务只执行一次onCreate方法,但是每次调用一次startService就会执行一次onStartCommand函数。
[b]2.注册服务类[/b]
在AndroidManifest.xml中的application字段中加入如下信息来注册这个服务类。
这边有一点非常重要的是 android:process="system" ,设置为system,否则按退出键使用如下方式来执行会导致程序崩溃,而且服务也会被终止,
因为service是和主线程在一起的,主线程被终止了,服务线程也会停止掉,就无法在后台执行了,所以我们必须把服务注册到系统中。
我们启动服务使用startservice方式,因为使用bindservice会跟所绑定的context一起死亡的,bindservice的概念是"不求同生,但求同死",所以使用bindservice时候注意不能设置android:process="system"
工程源码
1.首先我们需要用到Service类。
先上代码在慢慢解释
package com.example.androidnotification; import java.util.Timer; import java.util.TimerTask; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class PushService extends Service { static Timer timer = null; //清除通知 public static void cleanAllNotification() { NotificationManager mn= (NotificationManager) MainActivity.getContext().getSystemService(NOTIFICATION_SERVICE); mn.cancelAll(); if (timer != null) { timer.cancel(); timer = null; } } //添加通知 public static void addNotification(int delayTime,String tickerText,String contentTitle,String contentText) { Intent intent = new Intent(MainActivity.getContext(), PushService.class); intent.putExtra("delayTime", delayTime); intent.putExtra("tickerText", tickerText); intent.putExtra("contentTitle", contentTitle); intent.putExtra("contentText", contentText); MainActivity.getContext().startService(intent); } public void onCreate() { Log.e("addNotification", "===========create======="); } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } public int onStartCommand(final Intent intent, int flags, int startId) { long period = 24*60*60*1000; //24小时一个周期 int delay=intent.getIntExtra("delayTime",0); if (null == timer ) { timer = new Timer(); } timer.schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub NotificationManager mn= (NotificationManager) PushService.this.getSystemService(NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(PushService.this); Intent notificationIntent = new Intent(PushService.this,MainActivity.class);//点击跳转位置 PendingIntent contentIntent = PendingIntent.getActivity(PushService.this,0,notificationIntent,0); builder.setContentIntent(contentIntent); builder.setSmallIcon(R.drawable.ic_launcher); builder.setTicker(intent.getStringExtra("tickerText")); //测试通知栏标题 builder.setContentText(intent.getStringExtra("contentText")); //下拉通知啦内容 builder.setContentTitle(intent.getStringExtra("contentTitle"));//下拉通知栏标题 builder.setAutoCancel(true); builder.setDefaults(Notification.DEFAULT_ALL); Notification notification = builder.build(); mn.notify((int)System.currentTimeMillis(),notification); } },delay, period); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy(){ Log.e("addNotification", "===========destroy======="); super.onDestroy(); } }
自定义了一个类PushService继续Service,定义了两个类来实现添加通知和取消通知
//delayTime 延迟多久执行。
//tickerText
//contentTitle 通知栏的标题
//contentText 通知栏的内容
addNotification(int delayTime,String tickerText,String contentTitle,String contentText)
//清除通知
cleanAllNotification()
====================================
Service的启动,startService来启动服务只执行一次onCreate方法,但是每次调用一次startService就会执行一次onStartCommand函数。
[b]2.注册服务类[/b]
在AndroidManifest.xml中的application字段中加入如下信息来注册这个服务类。
<service android:enabled="true" android:name=".PushService" android:process="system"></service>
这边有一点非常重要的是 android:process="system" ,设置为system,否则按退出键使用如下方式来执行会导致程序崩溃,而且服务也会被终止,
android.os.Process.killProcess(android.os.Process.myPid());或者System.exit(0)
因为service是和主线程在一起的,主线程被终止了,服务线程也会停止掉,就无法在后台执行了,所以我们必须把服务注册到系统中。
我们启动服务使用startservice方式,因为使用bindservice会跟所绑定的context一起死亡的,bindservice的概念是"不求同生,但求同死",所以使用bindservice时候注意不能设置android:process="system"
工程源码
相关文章推荐
- android双后台服务,消息通知类
- android 后台服务、通知信息
- android 双后台服务,消息通知 (mainActivity)
- android双后台服务,消息通知类(service2)
- android后台服务service全解析(中)--IntentService与Notification前台通知
- Android程序后台开启服务,显示通知栏
- Android中后台服务与通知的协作工作
- Android中(Service )服务的最佳实践——后台执行的定时任务
- android双后台服务,消息通知类(service1)
- Android 软件升级(后台服务 + 通知栏进度条)
- android java开发 第四天 之(后台服务处理完业务数据后,传送给ui Activity并刷新界面)
- android本地后台服务示例
- Android程序 类QQ通知常驻在Notification栏 点击Notification正确回调到之前已经放置在后台的Task中的对应Activity,而不是创建它的一个新实例
- android判断某服务是否正在后台运行
- Android 如何屏蔽联通服务信息WAP通知信息http://g.iuni.com.cn, 百度无耻不让发贴批拼联通
- Android中后台定时任务实现,即时数据同步问题思考!
- Android中使用定时服务
- Android 应用五大核心块 --- Service后台服务
- android 后台服务多任务下载界面刷新机制
- Window Service 创建:在后台执行一个服务,可定时做一些操作,如轮询数据库,定时发邮件