1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建
@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }----------------
  @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
                // TODO Auto-generated method stub
                Log.v("TrafficService","startCommand");
                
                flags =  START_STICKY;
                return super.onStartCommand(intent, flags, startId);
//                return  START_REDELIVER_INTENT;
        }
2.在Service的onDestroy()中重启Service.
public void onDestroy() {   
        Intent localIntent = new Intent();
        localIntent.setClass(this, MyService.class);  //销毁时重新启动Service
        this.startService(localIntent);
    }---------------------------------------------
用qq管家杀掉进程的时候,调用的是系统自带的强制kill功能(即settings里的),在kill时,会将应用的整个进程停掉,当然包括service在内,如果在running里将service强制kill掉,显示进程还在。不管是kill整个进程还是只kill掉进应用的 service,都不会重新启动service。不知道你是怎么怎么实现重启的,实在是不解。。。
ps:在eclipse中,用stop按钮kill掉进程的时候,倒是会重启service
KILL问题:
1. settings 中stop service
onDestroy方法中,调用startService进行Service的重启。
2.settings中force stop 应用
捕捉系统进行广播(action为android.intent.action.PACKAGE_RESTARTED)
3. 借助第三方应用kill掉running task
提升service的优先级
--------------------------------------------
service开机启动

http://blog.csdn.net/flying_vip_521/article/details/7053355

  • 今天我们主要来探讨android怎么让一个service开机自动启动功能的实现。Android手机在启动的过程中会触发一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED(记得只会触发一次呀),在这里我们可以通过构建一个广播接收者来接收这个这个action.下面我就来简单写以下实现的步骤:
  • 第一步:首先创建一个广播接收者,重构其抽象方法 onReceive(Context context, Intent intent),在其中启动你想要启动的Service或app。
    • import android.content.BroadcastReceiver;
    • import android.content.Context;
    • import android.content.Intent;
    • import android.util.Log;
    • public class BootBroadcastReceiver extends BroadcastReceiver {
    • //重写onReceive方法
    • @Override
    • public void onReceive(Context context, Intent intent) {
    • //后边的XXX.class就是要启动的服务
    • Intent service = new Intent(context,XXXclass);
    • context.startService(service);
    • Log.v("TAG", "开机自动服务自动启动.....");
    • //启动应用,参数为需要自动启动的应用的包名
    • Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
    • context.startActivity(intent );
    • }
    • }
  • 第二步:配置xml文件,在receiver接收这种添加intent-filter配置
  • <receiver android:name="BootBroadcastReceiver">
  • <intent-filter>
  • <action android:name="android.intent.action.BOOT_COMPLETED"></action>
  • <category android:name="android.intent.category.LAUNCHER" />
  • </intent-filter>
  • </receiver>
  • 第三步:添加权限 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

---------------------------------------------------
如何实现一个不会被杀死的进程
看Android的文档知道,当进程长期不活动,或系统需要资源时,会自动清理门户,杀死一些Service,和不可见的Activity等所在的进程。
但是如果某个进程不想被杀死(如数据缓存进程,或状态监控进程,或远程服务进程),应该怎么做,才能使进程不被杀死。

add android:persistent="true" into the <application> section in your AndroidManifest.xml

切记,这个不可滥用,系统中用这个的service,app一多,整个系统就完蛋了。
目前系统中有phone等非常有限的,必须一直活着的应用在试用。

------------------------------------------------
提升service优先级的方法

Android 系统对于内存管理有自己的一套方法,为了保障系统有序稳定的运信,系统内部会自动分配,控制程序的内存使用。当系统觉得当前的资源非常有限的时候,为了保 证一些优先级高的程序能运行,就会杀掉一些他认为不重要的程序或者服务来释放内存。这样就能保证真正对用户有用的程序仍然再运行。如果你的 Service 碰上了这种情况,多半会先被杀掉。但如果你增加 Service 的优先级就能让他多留一会,我们可以用 setForeground(true) 来设置 Service 的优先级。

  为什么是 foreground ? 默认启动的 Service 是被标记为 background,当前运行的 Activity 一般被标记为 foreground,也就是说你给 Service 设置了 foreground 那么他就和正在运行的 Activity 类似优先级得到了一定的提高。当让这并不能保证你得 Service 永远不被杀掉,只是提高了他的优先级。 
  从Android 1.5开始,一个已启动的service可以调用startForeground(int, Notification)将service置为foreground状态,调用stopForeground(boolean)将service置为 background状态。 
  我们会在调用startForeground(int, Notification)传入参数notification,它会在状态栏里显示正在进行的foreground service。background service不会在状态栏里显示。 
  在Android 1.0中,将一个service置为foreground状态: 
  setForeground(true); 
  mNM.notify(id, notification); 
  将一个service置为background状态: 
  mNM.cancel(id); 
  setForeground(false); 
  对比看出,在1.0 API中调用setForeground(boolean)只是简单的改变service的状态,用户不会有任何觉察。新API中强制将 notification和改变service状态的动作绑定起来,foreground service会在状态栏显示,而background service不会。 
  Remote service controller & binding 
  跨进程调用Service。暂时不研究。 
-------------------------------------------------------
如何防止Android应用中的Service被系统回收?         很多朋友都在问,如何防止Android应用中的Service被系统回收?下面简单解答一下。

对于Service被系统回收,一般做法是通过提高优先级可以解决,在AndroidManifest.xml文件中对于intent-filter可以通过android:priority = "1000"这个属性设置最高优先级,1000是最高值,如果数字越小则优先级越低,同时实用于广播,推荐大家如果你的应用很重要,可以考虑通过系统常用intent action来触发。

android避免service被杀的更多相关文章

  1. Android Foreground Service (前台服务)

    一.如何保活后台服务 在Android Services (后台服务) 里面,我们了解了Android四大组件之一的Service,知道如何使用后台服务进行来完成一些特定的任务.但是后台服务在系统内存 ...

  2. Android服务(Service)研究

    Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...

  3. Android中Service 使用详解(LocalService + RemoteService)

    Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  4. android 入门-Service实时向Activity通过BroadcastReceiver传递数据

    引文: http://www.cnblogs.com/linjiqin/p/3147764.html <RelativeLayout xmlns:android="http://sch ...

  5. android 入门-Service

    sdk 1.7 package com.example.hellowrold; import java.util.Random; import com.example.hellowrold.R.id; ...

  6. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  7. Android中Service的使用

    我个人的理解是:我们平时使用的android系统的app的后台应用,就是这个原理 可以利用Service实现程序在后台运行,依照这个原理,可以通过Service来实现关键代码的运行与实现. <一 ...

  8. 【Android 】Service 全面总结

    1.Service的种类 按运行地点分类: 类别 区别  优点 缺点   应用 本地服务(Local) 该服务依附在主进程上,  服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  9. Android Activity/Service/Broadcaster三大组件之间互相调用

    我们研究两个问题,1.Service如何通过Broadcaster更改activity的一个TextView.(研究这个问题,考虑到Service从服务器端获得消息之后,将msg返回给activity ...

随机推荐

  1. JQuery动态增加删除元素

    <form action="" method="post" enctype="multipart/form-data"> < ...

  2. C#之out与ref的共性与区别以及用法

    引入: 首先看一个例子: class Program { static void Main(string[] args) { ; int result = Test(number); Console. ...

  3. WINDOWS系统下环境变量PATH和CLASSPATH的意思

    1 PATH 对于没有包含路径的命令,WINDOWS系统会默认去Windows 目录(C:\windows)和系统目录(C:\windows\system32)查找,如果没有找到,就去PATH变量内包 ...

  4. C# 导出word文档及批量导出word文档(2)

    aspose.word主要是通过把读取出来的数据放到datatable里,在datable里做相应的格式的调整,再导出到word文档里.mvc和webform最后导出的语句略有不同,在mvc的cont ...

  5. Javascript 判断浏览器是否为IE的最短方法

    作者:idd.chiang 发布时间:April 29, 2010 分类:Javascript/AS 在网上有幸看到夷人通过IE与非IE浏览器对垂直制表符支持特性搞出的一段简短的条件: var ie ...

  6. python 中变量的命名规范

    出自:http://www.diybl.com/course/3_program/python/20111130/563643.html 模块名: 小写字母,单词之间用_分割 ad_stats.py ...

  7. FileZilla Server下载以及安装使用

    新版本filezilla server已经不能在windows xp和windows20003下使用了 下面是可以在xp和2003下使用的最后版本下载地址 http://pan.baidu.com/s ...

  8. C程序设计语言练习题1-9

    练习1-9 编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替. 代码如下: #include <stdio.h> // 包含标准库的信息. int main() // ...

  9. UITableView常用属性和方法 - 永不退缩的小白菜

    UITableView常用属性和方法 - 永不退缩的小白菜 时间 2014-05-27 01:21:00  博客园精华区原文  http://www.cnblogs.com/zhaofucheng11 ...

  10. 基于htmlparser实现网页内容解析

    基于htmlparser实现网页内容解析 网页解析,即程序自动分析网页内容.获取信息,从而进一步处理信息. 网页解析是实现网络爬虫中不可缺少而且十分重要的一环,由于本人经验也很有限,我仅就我们团队开发 ...