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 不被杀死并提高优先级的更多相关文章

  1. 保证Android后台不被杀死的几种方法

    由于各种原因,在开发Android应用时会提出保证自己有一个后台一直运行的需求,如何保证后台始终运行,不被系统因为内存低杀死,不被任务管理器杀死,不被软件管家等软件杀死等等还是一个比较困难的问题.网上 ...

  2. 【Android】详解Android Service

    目录结构: contents structure [+] Service简单概述 Service在清单文件中的声明 Service启动服务 Service绑定服务 扩展Binder类 使用Messen ...

  3. 浅谈android Service和BroadCastReceiver

    1.题记 Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序. 广播接收者(BroadcastRece ...

  4. Android Service(上)

    一 Service简介 Service是Context的子类 Service是四大组件之一 用来在后台处理一些比较耗时的操作或者去执行某些需要长期运行的任务 二 注意 Service里面不能直接执行耗 ...

  5. Android Service使用简单介绍

    作为一个android初学者,经常对service的使用感到困惑.今天结合Google API 对Service这四大组件之一,进行简单使用说明. 希望对和我一样的初学者有帮助,如有不对的地方,也希望 ...

  6. android service两种启动方式

    android service的启动方式有以下两种: 1.Context.startService()方式启动,生命周期如下所示,启动时,startService->onCreate()-> ...

  7. 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 ...

  8. Android Service完全解析,关于服务你所需知道的一切(下)

    转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...

  9. Android Service完全解析,关于服务你所需知道的一切(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...

随机推荐

  1. 最近公共祖先-三(RMQ-ST)

    描述 上上回说到,小Hi和小Ho使用了Tarjan算法来优化了他们的"最近公共祖先"网站,但是很快这样一个离线算法就出现了问题:如果只有一个人提出了询问,那么小Hi和小Ho很难决定 ...

  2. ubuntu16.04中将python3设置为默认+永久去除Ubuntu16.04报错

    直接执行这两个命令: sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo updat ...

  3. github 获取 token

    登录github 地址:https://github.com 点击settings 在点击Developer settings 继续 继续 描述栏随意写  复选框是token的权限 都选上吧 点击红框 ...

  4. 遇到的Ajax相关问题

  5. Leetcode 310.最小高度树

    最小高度树 对于一个具有树特征的无向图,我们可选择任何一个节点作为根.图因此可以成为树,在所有可能的树中,具有最小高度的树被称为最小高度树.给出这样的一个图,写出一个函数找到所有的最小高度树并返回他们 ...

  6. 两个很实用很方便的函数核心及用法{(lower_bound)+(max_element))~~

    (1)            关于 lower_bound(a,a+n,x)-a的用法:                                                求x在数组a中的 ...

  7. Archiving not possible: No primary destinations errors

    If space ran out in an archive destination, after you fix the problem, you may still recieve the fol ...

  8. COJ 1208 矩阵快速幂DP

    题目大意: f(i) 是一个斐波那契数列 , 求sum(f(i)^k)的总和 由于n极大,所以考虑矩阵快速幂加速 我们要求解最后的sum[n] 首先我们需要思考 sum[n] = sum[n-1] + ...

  9. 《TCP/IP详解卷1:协议》——第3章 IP:网际协议(转载)

    1.引言 IP是TCP/IP协议族中最核心的协议.所有的TCP.UDP.ICMP及IGMP数据都以IP数据报格式传输.IP提供不可靠. 无连接的数据报传送服务. (1)不可靠 它不能保证IP数据报能成 ...

  10. MYSQL Explain语法

    Explain语法 EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可得 ...