Android Service 不被杀死并提高优先级
Android Service 不被杀死有两种思路,一种是将APP设置为系统应用。还有一种是增强service的生命力。即使屏幕背光关闭时也能执行。
因为设置为系统应用须要root。所以一般使用后一种方法:
1.Androidmanifest.xml权限许可:-----------------------------------------------------------------
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<service android:name="com.xx.MyService" ></service>
2.主Activity:----------------------------------------------------------------------------------------
private ServiceConnection conn=null;
private PowerManager.WakeLock mwl;
@Override
protected void
onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mian);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mwl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyTag");
mwl.acquire();//屏幕关闭后保持活动
Intent service=new Intent(this,MyService.class);
startService(service);
conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) { }
@Override
public void onServiceConnected(ComponentName name, IBinder service) { //……}
};
bindService(service, conn, BIND_AUTO_CREATE);//绑定服务
}
@Override
protected void onDestroy() {
//mwl.release();//释放
unbindService(conn);//解绑
super.onDestroy();
}
------------------------------------------------------------------------------------------------------
Android Service 提高优先级:
public class MyService extendsService{
@Override
public IBinder onBind(Intent intent) {
Notification notification = new Notification(R.drawable.ic_launcher, "title",System.currentTimeMillis());
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
notification.setLatestEventInfo(this, "Service", "service is running", pintent);
startForeground(0, notification);//设置最高级进程。 id 为 0状态栏的 notification 将不会显示
return new MyBinder();
}
class MyBinder extendsBinder{ //…… }
@Override
public void onDestroy() {
stopForeground(true);//取消最高级进程
super.onDestroy();
}
}
Android Service 不被杀死并提高优先级的更多相关文章
- 保证Android后台不被杀死的几种方法
由于各种原因,在开发Android应用时会提出保证自己有一个后台一直运行的需求,如何保证后台始终运行,不被系统因为内存低杀死,不被任务管理器杀死,不被软件管家等软件杀死等等还是一个比较困难的问题.网上 ...
- 【Android】详解Android Service
目录结构: contents structure [+] Service简单概述 Service在清单文件中的声明 Service启动服务 Service绑定服务 扩展Binder类 使用Messen ...
- 浅谈android Service和BroadCastReceiver
1.题记 Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序. 广播接收者(BroadcastRece ...
- Android Service(上)
一 Service简介 Service是Context的子类 Service是四大组件之一 用来在后台处理一些比较耗时的操作或者去执行某些需要长期运行的任务 二 注意 Service里面不能直接执行耗 ...
- Android Service使用简单介绍
作为一个android初学者,经常对service的使用感到困惑.今天结合Google API 对Service这四大组件之一,进行简单使用说明. 希望对和我一样的初学者有帮助,如有不对的地方,也希望 ...
- android service两种启动方式
android service的启动方式有以下两种: 1.Context.startService()方式启动,生命周期如下所示,启动时,startService->onCreate()-> ...
- 1、Android Studio集成极光推送(Jpush) 报错 java.lang.UnsatisfiedLinkError: cn.jpush.android.service.PushProtoco
Android studio 集成极光推送(Jpush) (华为手机)报错, E/JPush: [JPushGlobal] Get sdk version fail![获取sdk版本失败!] W/Sy ...
- Android Service完全解析,关于服务你所需知道的一切(下)
转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...
- Android Service完全解析,关于服务你所需知道的一切(上)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...
随机推荐
- [JOYOI] 1415 西瓜种植
题目描述 笨笨种了一块西瓜地,但这块西瓜地的种植范围是一条直线的-- 笨笨在一番研究过后,得出了m个结论,这m个结论可以使他收获的西瓜最多. 笨笨的结论是这样的: 从西瓜地B处到E处至少要种植T个西瓜 ...
- Linux基础学习-通过VM安装RHEL7.4
虚拟机安装RHEL7.4 1.VM虚拟机设置 这里我们配置的虚拟机为1核1G,50G硬盘,NAT模式 2.Linux安装 这里时区我们选择中国上海,时间需要调整一下相差8小时. 这里添加一下中文语言支 ...
- tcpcopy简单用法
这篇文章介绍下网易开源的流量重放(replay)工具TCPCopy,说是简单介绍,绝对不是谦虚,因为自己了解的确实也不多.为什么不甚了解呢,大家可以到TCPCopy的官方仓库看看,https://gi ...
- 大数据学习——shell编程
03/ shell编程综合练习 自动化软件部署脚本 3.1 需求 1.需求描述 公司内有一个N个节点的集群,需要统一安装一些软件(jdk) 需要开发一个脚本,实现对集群中的N台节点批量自动下载.安装j ...
- struts2接受请求参数
https://blog.csdn.net/y249839817/article/details/77702745 https://blog.csdn.net/nthack5730/article/d ...
- python018 Python3 输入和输出
Python3 输入和输出在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化Python两种输出值的方式: 表达式语句 ...
- vue.js基础知识总结
初始化一个项目 npm init -y 安装一些依赖 npm install 名称 --save 例如 npm install vue axios bootstrap --save --save 表示 ...
- 两行代码快速创建一个iOS主流UI框架
本框架适用于 使用 NavigationController+UITabBarController 的APP 框架QLSNavTab , GitHub地址:https://github.com/qia ...
- android开发里跳过的坑-电源锁WakeLock不起作用
由于个别需求,需要做一个不让系统屏常亮不休眠的APP,于是使用了电源锁,在oncreate里加了如下代码: PowerManager pm = (PowerManager) context .getS ...
- GDKOI2018游记
D0 开开心心去酒店,在Vanda,资磁,然而和其他人住的比较远,不资磁. 开开心心打开玩具熊,吓尿了..第四部贼难. 晚上看了看网络流,1点才睡.3点多好像梦到玩具熊被吓醒,4点继续睡,6点起. D ...