android 后台服务定时通知
最近有个项目的要求是在程序退出之后,任然可以每天定时发通知,我们可以想下,其实就是后台开一个服务,然后时间到了就发下通知。
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函数。
2.注册服务类
在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后台服务拍照
原文:https://blog.csdn.net/wurensen/article/details/47024961 一.背景介绍最近在项目中遇到一个需求,实现一个后台拍照的功能.一开始在网上寻找解决 ...
- Android后台服务拍照的解决方式
一.背景介绍 近期在项目中遇到一个需求.实现一个后台拍照的功能. 一開始在网上寻找解决方式.也尝试了非常多种实现方式,都没有惬意的方案.只是确定了难点:即拍照要先预览,然后再调用拍照方法.问题也随之而 ...
- Service官方教程(5)后台服务发送通知、把服务变前台服务。
1.Sending Notifications to the User (发送通知) Once running, a service can notify the user of events usi ...
- Android中如何像 360 一样优雅的杀死后台服务而不启动
Android中,虽然有很多方法(API或者shell命令)杀死后台`service`,但是仍然有很多程序几秒内再次启动,导致无法真正的杀死.这里主要着重介绍如何像 360 一样杀死Android后台 ...
- 答CsdnBlogger问-关于定时和后台服务问题
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 前段时间写了不少博客,在问答页面也陆续回答几十个问题,之后Csdn乙同学找到我,说要推荐我参加问答类 ...
- 服务 IntentService 前台服务 定时后台服务
Activity public class MainActivity extends ListActivity { private int intentNumber = 0; @Ove ...
- Android Services (后台服务)
一.简介 服务是可以在后台执行长时间运行的应用程序组件,它不提供用户界面. 另一个应用程序组件可以启动一个服务,并且即使用户切换到另一个应用程序,它仍然在后台运行. 另外,组件可以绑定到一个服务来与它 ...
- Android 三级联动选择城市+后台服务加载数据库
技术渣,大家将就着看 首先我们需要一个xml数据保存到数据库,这里我从QQ下面找到一个loclist.xml文件 <CountryRegion Name="中国" Code= ...
- Android : App客户端与后台服务的AIDL通信以及后台服务的JNI接口实现
一.APP客户端进程与后台服务进程的AIDL通信 AIDL(Android Interface definition language-“接口定义语言”) 是 Android 提供的一种进程间通信 ( ...
随机推荐
- UVA - 11400 Lighting System Design (区间DP)
这个问题有两个点需要注意: 1. 对于一种灯泡,要么全换,要么全不换. 证明: 设一种灯泡单价为p1,电池价格为k1,共需要L个,若把L1个灯泡换成单价为p2,电池为k2的灯泡,产生的总花费为p1*L ...
- Oracle Sql优化之报表和数据仓库运算
1.行转列:有两种写法,一种是case when end写法,另一种写法是pivot(oracle 11g新增) select job, then sal end) as sal10, then sa ...
- 将项目同时托管到Github和Git@OSC
http://my.oschina.net/GIIoOS/blog/404555?fromerr=KHvn8UKH 摘要 Github是最大的git代码托管平台,GIT@OSC是国内最大的git代码 ...
- TCP/IP模型各个层次的功能和协议
层次名称 功 能 协 议 应用层 (Application Layer) 负责实现一切与应用程序相关的功能,对应OSI参考模型的上三层 FTP(文件传输协议) HTTP(超文本传输协议 ...
- 剑指offer 二叉搜索树后续遍历序列 判断
最后一个元素是 根节点. 左子树的元素都小于根节点,右子树都大于根节点 然后递归判断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- CentOS/RHEL 7中的firewall控制
从CentOS/RHEL 7开始firewall的使用.很多人卸载了firewall重装iptables.但是有时候只是为了开放端口什么的,没有那个闲工夫卸载重装: 永久打开一个新端口(如TCP/80 ...
- UILabel 详解
转自:http://blog.csdn.net/zhaopenghhhhhh/article/details/16331041 ·UILable是iPhone界面最基本的控件,主要用来显示文本信息.· ...
- opencart添加任意页面到导航栏(修改一级菜单)
pencart默认的黑色导航栏只显示分类目录下的一级分类,这块整个网页中最显眼的“风水宝地”怎么能让他闲置呢,因此我想到了为opencart导航栏添加自定义页面,它可以连接到任意一个网址或者ope ...
- 基于Spring的异步系统实现方案
一般的实现方案 发送异步消息所使用的工具类: import java.util.Date; import javax.jms.Destination; import javax.jms.JMSExce ...
- thinkphp5.0 架构
1.0 MVC的意义:MVC是一个设计模式,它强制性的使应用程序的输入.处理和输出分开.使用MVC应用程序被分成三个核心部件:模型(M).视图(V).控制器(C),它们各自处理自己的任务. 2.0入口 ...