1.Service 前台服务与Notification

我们在用很多应用的时候,发现他们启动的时候,会在通知栏生成一个和该App的通知,来继续执行Service,比如墨迹天气,很多音乐App.这种叫前台服务,其实这种Service有一个很好的一点,就是不会因为Service自身的优先级低,而被系统KILL,而前台服务就不会。
前台服务的写法很容易,只需要在onCreate()中,建立一个通知,然后用startForeground()设置为前台服务即可。
下面直接放出代码,结合代码注释看看就好了,关于通知更多的内容可以看看
这里只列出Service的onCreate()部分代码

@Override
public void onCreate() {
super.onCreate();
//设定一个PendingIntent,来表示点击通知栏时跳转到哪里
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
Notification.Builder builder = new Notification.Builder(this);
//建立一个notificationManager来管理通知的出现
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//构造通知的样式,包括图片,标题,内容,时间。
builder.setSmallIcon(R.mipmap.ic_launcher).
setWhen(System.currentTimeMillis()).
setContentTitle("我是标题").
setContentText("我是内容").
setTicker("在启动时弹出一个消息").//这个Android5.0以上可能会失效
setWhen(System.currentTimeMillis()).
setContentIntent(contentIntent);
//最后通过build建立好通知
Notification notification = builder.build();
//通过manager来显示通知,这个1为notification的id
notificationManager.notify(1,notification);
//启动为前台服务,这个1为notification的id
startForeground(1,notification);
}

2.后台定时服务

后台定时服务其实并不是特殊的Service,只是Service的常见的一种应用,放到后台做定时更新,轮询等。这次的Service要配合Alarm以及简单的广播机制来实现。
步骤主要如下:

第一步 获得Service

AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);

第二步 通过set方法设置定时任务

int time = 1000;
long triggerAtTime = SystemClock.elapsedRealtime() + time;
Intent i = new Intent(this,AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);

第三步 定义一个Service,在onStartCommand中开辟一个事务线程,用于处理一些定时逻辑

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
//执行想做的操作,比如输出时间
}
}).start();
//步骤二里面的代码
return super.onStartCommand(intent, flags, startId);
}

第四步 定义一个Broadcast,用于启动Service

public class AlarmReceiver extends BroadcastReceiver {

    @Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context,LongRunningService.class);
context.startService(i);
}
}

  

  

  

Android 手机卫士12--进程管理的更多相关文章

  1. Android 手机卫士10--应用管理器

    1.添加不同类型条目 class MyAdapter extends BaseAdapter{ //获取数据适配器中条目类型的总数,修改成两种(纯文本,图片+文字) @Override public ...

  2. android手机卫士、3D指南针、动画精选、仿bilibli客户端、身份证银行卡识别等源码

    Android精选源码 android身份证.银行卡号扫描源码 android仿bilibili客户端 android一款3D 指南针 源码 android手机卫士app源码 android提醒应用, ...

  3. Android 手机卫士--参照文档编写选择器

    本文来实现<Android 手机卫士--导航界面1的布局编写>中的图片选择器部分的代码. 本文地址:http://www.cnblogs.com/wuyudong/p/5944356.ht ...

  4. Android 手机卫士--设置界面&功能列表界面跳转逻辑处理

    在<Android 手机卫士--md5加密过程>中已经实现了加密类,这里接着实现手机防盗功能 本文地址:http://www.cnblogs.com/wuyudong/p/5941959. ...

  5. Android 手机卫士--确认密码对话框编写

    本文接着实现“确认密码”功能,也即是用户以前设置过密码,现在只需要输入确认密码 本文地址:http://www.cnblogs.com/wuyudong/p/5940718.html,转载请注明出处. ...

  6. Android 手机卫士--签名文件说明&包名说明

    在<Android 手机卫士--打包生成apk维护到服务器>一文中,实现了新版本的apk到服务器,当打开客户端apk的时候,发现有新版本,提示更新.还实现了利用xutils工具实现了从服务 ...

  7. Android 手机卫士--弹出对话框

    在<Android 手机卫士--解析json与消息机制发送不同类型消息>一文中,消息机制发送不同类型的信息还没有完全实现,在出现异常的时候,应该弹出吐司提示异常,代码如下: private ...

  8. Android 手机卫士--九宫格使用

    本文地址:http://www.cnblogs.com/wuyudong/p/5907736.html,转载请注明源地址. 采用GridView来实现,和ListView使用方式类似,列数(3列) 首 ...

  9. [android] 手机卫士欢迎细节和主界面

    splash界面的细节 ctrl + O 搜索 在去标题的时候,对话框主题被去掉了,有点丑,现在既要有新版本的对话框又不显示标题 把清单文件中activity节点的主题去掉 进入到applicatio ...

随机推荐

  1. [LeetCode] Sparse Matrix Multiplication

    Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that ...

  2. preg_match_all正则表达式的基本使用

    了解正则表达式之前,须要掌握一些常用的正则表达式的基础知识,这些如果能记得最好记得,记不住须要用的时候能查到就行,就多个特殊字符,所以说正则表达式玩的就是特殊,具体大家可以查看更加细致的说明. pre ...

  3. RadioButton 的background属性表现特征

    对于radiaoButton,应该很多人都用过.下面看一个场景     上方时radiogroup,细致观察发现左1,文字开始位置和右1文字开始位置不同,这是为何呢? 查看布局: <RadioB ...

  4. python 集合set

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...

  5. 重新发现梯度下降法--backtracking line search

    一直以为梯度下降很简单的,结果最近发现我写的一个梯度下降特别慢,后来终于找到原因:step size的选择很关键,有一种叫backtracking line search的梯度下降法就非常高效,该算法 ...

  6. (转)关于tcp和udp的缓冲区

    (一)基础知识 IPv4 数据报最大大小是65535(16位),包括IPv4头部. IPv6 数据报最大大小是65575,包括40个字节的IPv4头部 MTU,这是由硬件规定的,如以太网的MTU是15 ...

  7. ireport 导出工具类

    Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,html 格式 下面是报表导出工具类 Ireport 报表导出 Poi + ireport 导出pdf,  ...

  8. Using HiveServer2 - Authentication

    To configure Hive for use with HiveServer2, include the following configuration properties in the .. ...

  9. 模拟MessageBox

    原文:<模拟MessageBox> Posted on 2014/01/07 ======================================================= ...

  10. js中的包装对象。

    我们都知道在js中普通类型的变量是没有属性和方法的,然后有时却并不是这样. var str = "努力,奋斗"; console.log(str.length); var num ...