android8.1启动前台服务,Android - 保活(1)前台服务保活
老婆保佑,代码无BUG
前言
项目中遇到一个需求,需要竟可能的上传用户的定位信息,引发了我对目前已知的保活手段的探究,同时也遇到过客户说,推送不能收到,不能像微信那样,MMP的,不想理客户
目录
一:如何创建前台服务
1.DeskService 前台服务
2.移除前台Service通知栏标志
3.注册服务
4.启动服务
二:查看adj级别
一:如何创建前台服务
1.DeskService 前台服务
package com.cpsc.livedemo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* 描述:
*
* Created by allens on 2018/1/31.
*/
public class DeskService extends Service {
private static final String TAG = "DaemonService";
public static final int NOTICE_ID = 100;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "DaemonService---->onCreate被调用,启动前台service");
//如果API大于18,需要弹出一个可见通知
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("KeepAppAlive");
builder.setContentText("DaemonService is runing...");
startForeground(NOTICE_ID, builder.build());
// 如果觉得常驻通知栏体验不好
// 可以通过启动CancelNoticeService,将通知移除,oom_adj值不变
Intent intent = new Intent(this, CancelNoticeService.class);
startService(intent);
} else {
startForeground(NOTICE_ID, new Notification());
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 如果Service被终止
// 当资源允许情况下,重启service
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
// 如果Service被杀死,干掉通知
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mManager.cancel(NOTICE_ID);
}
Log.d(TAG, "DaemonService---->onDestroy,前台service被杀死");
// 重启自己
Intent intent = new Intent(getApplicationContext(), DeskService.class);
startService(intent);
}
}
2.移除前台Service通知栏标志
package com.cpsc.livedemo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
/**
* 描述:
*
* Created by allens on 2018/1/31.
*/
public class CancelNoticeService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
startForeground(DeskService.NOTICE_ID, builder.build());
// 开启一条线程,去移除DaemonService弹出的通知
new Thread(new Runnable() {
@Override
public void run() {
// 延迟1s
SystemClock.sleep(1000);
// 取消CancelNoticeService的前台
stopForeground(true);
// 移除DaemonService弹出的通知
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(DeskService.NOTICE_ID);
// 任务完成,终止自己
stopSelf();
}
}).start();
}
return super.onStartCommand(intent, flags, startId);
}
}
3.注册服务
android:name=".DeskService"
android:enabled="true"
android:exported="true"
android:process=":daemon_service" />
android:name=".CancelNoticeService"
android:enabled="true"
android:exported="true"
android:process=":service" />
4.启动服务
startService(new Intent(this, DeskService.class));
二:查看adj级别
adb shell
ps|grep 查看基本信息
1|root@generic_x86:/ # ps|grep com.cpsc.livedemo
u0_a63 6834 1348 1285208 43884 SyS_epoll_ b73712b5 S com.cpsc.livedemo
u0_a63 6884 1348 1271160 28944 SyS_epoll_ b73712b5 S com.cpsc.livedemo:daemon_service
值
解释
u0_a63
USER 进程当前用户
6834
进程ID
1348
进程的父进程ID
1285208
进程的虚拟内存大小
43884
实际驻留”在内存中”的内存大小
com.cpsc.livedemo
进程名
cat /proc//oom_adj
root@generic_x86:/ # cat /proc/6884/oom_adj
1
root@generic_x86:/ # cat /proc/6884/oom_adj
1
root@generic_x86:/ #
adj级别
值
说明
UNKNOWN_ADJ
16
预留的最低级别,一般对于缓存的进程才有可能设置成这个级别
CACHED_APP_MAX_ADJ
15
缓存进程,空进程,在内存不足的情况下就会优先被kill
CACHED_APP_MIN_ADJ
9
缓存进程,也就是空进程
SERVICE_B_ADJ
8
不活跃的进程
PREVIOUS_APP_ADJ
7
切换进程
HOME_APP_ADJ
6
与Home交互的进程
SERVICE_ADJ
5
有Service的进程
HEAVY_WEIGHT_APP_ADJ
4
高权重进程
BACKUP_APP_ADJ
3
正在备份的进程
PERCEPTIBLE_APP_ADJ
2
可感知的进程,比如那种播放音乐
VISIBLE_APP_ADJ
1
可见进程
FOREGROUND_APP_ADJ
0
前台进程
PERSISTENT_SERVICE_ADJ
-11
重要进程
PERSISTENT_PROC_ADJ
-12
核心进程
SYSTEM_ADJ
-16
系统进程
NATIVE_ADJ
-17
系统起的Native进程
android8.1启动前台服务,Android - 保活(1)前台服务保活相关推荐
- android打开位置服务,Android - 位置定位(Location)服务(Service)类的基本操作
位置定位(Location)服务(Service)类的基本操作 本文地址: http://blog.csdn.net/caroline_wendy 定位服务(Location Service),能够确 ...
- android+硬件服务,android之硬件访问服务框架
一.硬件接口描述文件aidl 新增\frameworks\base\core\java\android\os\ILedService.aidl 二.mk编译脚本 修改vi frameworks/bas ...
- android四大组件 服务,Android四大组件之Service
Service Service(服务)是一个可以在后台执行长时间运行操作而不使用用户界面的应用组件.服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行. 此外,组件可以绑定到 ...
- 【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 启动相同 id 的第二个前台 Service 关闭通知 )
文章目录 一. 前台 Service 通知问题 二. 设置 startForeground id 参数为 0 三. 启动相同 id 的第二个前台 Service 关闭通知 1. 前台服务 1 2. 关 ...
- Android Foreground Service (前台服务)-by:nixs
一.如何保活后台服务 在Android Services (后台服务) 里面,我们了解了Android四大组件之一的Service,知道如何使用后台服务进行来完成一些特定的任务.但是后台服务在系统内存 ...
- android Foreground Service 前台服务/notification全局通知
android Foreground Service 前台服务/notification全局通知 前言 要素简介 前台服务(Foreground Service) 全局通知(notification) ...
- android usb attached,Android USB主机模式服务 – 基于USB_DEVICE_ATTACHED启动
我想在Android中编写一个基于USB_DEVICE_ATTACHED意图启动的服务.因此,基本上我的服务应该在连接特定USB设备(FT232C – VID:PID 0403:6010)时启动,并在 ...
- Android中启动一个服务,Android服务的两种启动方式
前言:Service是Android的四大组件之一,也是可执行的程序.服务是Android中实现程序后台运行的解决方案,非常适合去执行那些不需要和用户交互而且还要求长期运行的任务.Service的启动 ...
- 什么是Android进程(app)保活、进程保活的方案
想了解什么是Android 进程.Android 进程的生命周期.Android 进程回收策略 可参照地址什么是Android 进程.Android 进程的生命周期.Android 进程回收策略_lm ...
最新文章
- CV领域最经典的Paper是什么来头?
- 使用ASV2011研究Molehill API
- 做专才能做强做大——从OA、协同之争说起
- 字典对中文字符串进行排序 python_Python基础入门:字符串和字典
- java分行符号怎么打_Android string.xml如何输入空格、换行等符号——转义字符
- ABAP RANGE 实现 SELECTION-OPTION 功能
- java基础知识1---面向对象及final,finally,finalize区别
- 如何在linux系统自动mount一个NTFS分区只读方式挂载
- Android Gallery和ImageSwitcher同步自动(滚动)播放图片库
- 智能卡门禁管理系统_出入口门禁控制系统与消防火灾报警系统怎么联动?
- sklearn 2.回归树
- Hadoop HBase
- 天猫标的就是虚价,果然败家节啊
- 【Flutter】Dart中的构造函数
- Iper3打流时所支持的UDP协议是真的UDP吗?
- 算法 动画算法 算法动画 动画原理——总体概览图
- 芯片设计流程 芯片的设计原理图
- 【云计算的1024种玩法】搭建 wiki 知识库
- 运用supervisor管理thinkqueue 和swoole推送
- 南丁格尔玫瑰图 With ggplot2【R语言】