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 ...
随机推荐
- ssh服务使用介绍二
老司机开车了 快上车 上文提到当我们使用ssh远程链接主机的时候,会出现询问的提示,如果我们链接的机器多了,一遍一遍的输入是不是很麻烦?怎么解决如下 vim /etc/ssh/ssh_config 如 ...
- 一篇文章掌握nightwatch自动化测试
nightwatch.js是一个web-ui自动化测试框架,被vue-cli深度整合进来.如果一个项目是基于vue-cli搭建的,基本可以做到开箱即用. 但是我们不可能一直都使用vue-cli.因为它 ...
- hdu1394(Minimum Inversion Number)线段树
明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...
- shiro框架的四中权限控制方式
https://www.cnblogs.com/cocosili/p/7103025.html 一.在自定义的realm中进行权限控制 在applicationContext.xml文件中添加 /a ...
- [工具]Visual Studio
1,Tab键的使用: 如不说有这样的代码:public Member member { get; set; } 当我们编辑完Member后,按一下Tab键,就能够将光标锁定到member上,等待键盘输 ...
- 【01】恶趣味玩转 GitHub commit 历史记录
[黑科技]恶趣味玩转 GitHub commit 历史记录 工具:https://github.com/gelstudios/gitfiti 效果截图:预览地址:https://github.co ...
- jQuery+ajax城市联动
分享一下自己最近写的城市联动.技术使用ajax+jQuery实现. 首先请看前台的javascript代码. 以下是连个实现异步加载的方法. <script type="text/ja ...
- zoj 1949 Error Correction
Error Correction Time Limit: 2 Seconds Memory Limit: 65536 KB A boolean matrix has the parity p ...
- Git x SVN 当前工作流程
git-svn 当前工作流程 @ixenos 2018-12-27 21:37:47 前言:用惯了git,再用svn简直反人类,所以……还是用git-svn过渡一下 (由于远程还没有dev,直接坑爹地 ...
- HDU1754-I Hate It,线段数水题~~2016.4.11
I Hate It ...