您现在的位置: 爱51代码网 >> 范文 >> 文章正文
android如何做到后台服务不会被杀死
做锁屏肯定用到了service,可是我发现每日手动点击自带的内存清理按钮的时候,我的那个service总是会被杀死。
而微信的后台服务却是一直正常的运行,不会被杀掉。
360的话也不会被杀死,但是360会重新启动。而且360的是两个后台服务,我猜有可能会相互作用的,杀死一个的时候另一个接收到广播把其重启。
尝试过用startForeground以及提高service优先级的方式,发现都不行,service都会被杀死。
反编译了一下微信的代码。
请大家帮忙想想办法吧。谢了
下面是微信的注册service:
 
<service
            android:name="com.tencent.mm.booter.CoreService"
            android:process=":push"
            >
        </service>
 
public class CoreService extends Service
  implements com.tencent.mm.jni.platformcomm.f, af, bl
{
  private ao bZl;
  private ac bZm = new ac();
  private boolean bZn = true;
  public final int bZo = -1213;
  private final ai bZp = new e(this);
  private com.tencent.mm.modelstat.l bZq = null;
  private AddrBookObserver bZr;
  private WatchDogPushReceiver bZs;
  private WakerLock bZt = null;
  private m bZu = new m();
  private ay bZv = new ay(new g(this), false);
 
  private void oj()
  {
    aa.w("MicroMsg.CoreService", "[COMPLETE EXIT]");
    bk.Fg().d(3, 10000, "");
    bg.onDestroy();
    try
    {
      MMReceivers.AlarmReceiver.D(getApplicationContext());
      MMReceivers.AlarmReceiver.B(getApplicationContext());
      Alarm.H(getApplicationContext());
      label43: aa.appenderClose();
      Process.killProcess(Process.myPid());
      return;
    }
    catch (Exception localException)
    {
      break label43;
    }
  }
 
  public final void H(boolean paramBoolean)
  {
    if (!paramBoolean)
    {
      aa.w("MicroMsg.CoreService", "[NETWORK LOST]");
      bk.Fc().cFB = false;
      bk.Fd().eJ(0);
      bk.Fb().a(10502, "", null);
      this.bZn = false;
      return;
    }
    aa.w("MicroMsg.CoreService", "[NETWORK CONNECTED]");
    bk.Fc().cFB = true;
    boolean bool = this.bZm.oM();
    if ((this.bZn) && (!bool))
    {
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = Boolean.valueOf(this.bZn);
      aa.d("MicroMsg.CoreService", "network not change or can't get network info, lastStatus connect:%b", arrayOfObject);
      return;
    }
    if (bool)
      bk.Ff().EN();
    this.bZn = true;
    bk.Fd().eJ(1);
    bk.Fb().a(10501, "", null);
    if (this.bZt == null)
      this.bZt = new WakerLock(getApplicationContext());
    if (!this.bZt.isLocking())
      this.bZt.lock(14000L);
    aa.i("MicroMsg.CoreService", "checking ready, start in 7000ms");
    this.bZv.bO(7000L);
  }
 
  public final boolean a(int paramInt, byte[] paramArrayOfByte)
  {
    if (getSharedPreferences("system_config_prefs", com.tencent.mm.compatible.g.l.qi()).getBoolean("settings_fully_exit", true))
    {
      aa.i("MicroMsg.CoreService", "fully exited, no need to notify worker");
      return false;
    }
    Intent localIntent = new Intent(this, NotifyReceiver.class);
    localIntent.putExtra("notify_option_type", 2);
    localIntent.putExtra("notify_uin", this.bZl.ES().sd());
    localIntent.putExtra("notify_respType", paramInt);
    localIntent.putExtra("notify_respBuf", paramArrayOfByte);
    localIntent.putExtra("notfiy_recv_time", cj.FD());
    localIntent.putExtra("notify_skey", this.bZl.ES().uo());
    aa.i("MicroMsg.CoreService", "notify broadcast:" + localIntent.getAction() + ", type=" + paramInt);
    try
    {
      sendBroadcast(localIntent);
      return true;
    }
    catch (Exception localException)
    {
      while (true)
      {
        Object[] arrayOfObject = new Object[1];
        arrayOfObject[0] = localException.toString();
        aa.a("MicroMsg.CoreService", "onNotify hasDestroyed %s", arrayOfObject);
      }
    }
  }
 
  public IBinder onBind(Intent paramIntent)
  {
    aa.d("MicroMsg.CoreService", "onBind~~~ threadID:" + Thread.currentThread());
    return this.bZl;
  }
 
  public void onCreate()
  {
    aa.d("MicroMsg.CoreService", "onCreate~~~threadID:" + Thread.currentThread());
    super.onCreate();
    if (Build.VERSION.SDK_INT < 18)
      startForeground(-1213, new Notification());
    Handler localHandler = new Handler(Looper.getMainLooper());
    PlatformComm.a(al.getContext(), localHandler);
    i locali = new i(this);
    locali.om();
    ag.a(this.bZp);
    if (PlatformComm.cgz == null)
      PlatformComm.cgz = this;
    bk.a(localHandler);
    bk.setContext(getApplicationContext());
    bk.a(new bm());
    bk.a(new bn());
    bk.a(this);
    if (this.bZq == null)
      this.bZq = new com.tencent.mm.modelstat.l();
    bk.a(this.bZq);
    this.bZl = bk.Ff();
    if (this.bZl == null)
    {
      aa.i("MicroMsg.CoreService", "autoAuth is null and new one");
      this.bZl = new ao(bk.getHandler());
      bk.e(this.bZl);
      bg.onCreate();
      if (bk.Fg() != null)
        break label465;
      aa.i("MicroMsg.CoreService", "NetTaskAdapter is null and new one");
      bk.a(new bi());
      label228: if (bk.Fh() == null)
      {
        aa.i("MicroMsg.CoreService", "NetTaskAdapter is null and new one");
        bk.a(new bh());
        bk.Fh().cFn = this;
      }
      at.azE().a(new f(this), null);
      String str1 = locali.getString(".com.tencent.mm.debug.server.host.http");
      String str2 = locali.getString(".com.tencent.mm.debug.server.ports.http");
      String str3 = locali.getString(".com.tencent.mm.debug.server.host.socket");
      String str4 = locali.getString(".com.tencent.mm.debug.server.ports.socket");
      this.bZl.d(str1, str2, str3, str4);
      MMReceivers.AlarmReceiver.D(getApplicationContext());
      MMReceivers.AlarmReceiver.C(getApplicationContext());
      NetworkInfo localNetworkInfo = ((ConnectivityManager)getSystemService("connectivity")).getActiveNetworkInfo();
      if ((localNetworkInfo != null) && (localNetworkInfo.getState() == NetworkInfo.State.CONNECTED))
        break label482;
      bk.Fc().cFB = false;
      bk.Fd().eJ(0);
    }
    while (true)
    {
      this.bZr = new AddrBookObserver(this, new Handler());
      getContentResolver().registerContentObserver(c.aph(), true, this.bZr);
      this.bZs = new WatchDogPushReceiver();
      registerReceiver(this.bZs, new IntentFilter("com.tencent.mm.WatchDogPushReceiver"));
      return;
      aa.w("MicroMsg.CoreService", "autoAuth is not null and reset");
      this.bZl.reset();
      break;
      label465: aa.w("MicroMsg.CoreService", "NetTaskAdapter is not null and reset");
      bk.Fg().reset();
      break label228;
      label482: bk.Fc().cFB = true;
      bk.Fd().eJ(1);
    }
  }
 
  public void onDestroy()
  {
    aa.d("MicroMsg.CoreService", "onDestroy~~~ threadID:" + Thread.currentThread());
    this.bZq.a(10002, null, null);
    getContentResolver().unregisterContentObserver(this.bZr);
    unregisterReceiver(this.bZs);
    super.onDestroy();
    oj();
  }
 
  public void onRebind(Intent paramIntent)
  {
    aa.d("MicroMsg.CoreService", "onRebind~~~ threadID:" + Thread.currentThread());
    super.onRebind(paramIntent);
  }
 
  public boolean onUnbind(Intent paramIntent)
  {
    aa.d("MicroMsg.CoreService", "onUnbind~~~ threadID:" + Thread.currentThread());
    bk.Fc().a(null);
    return super.onUnbind(paramIntent);
  }
 
  public final void restartProcess()
  {
    aa.w("MicroMsg.CoreService", "restartProcess");
    oj();
  }
}
微信这个应用根本没有退出。而是在后台运行着,
微信使用的是bindService,由于应用没有退出。所以不会内存管理器被杀死。
android 系统杀程序,一个很重要的判断就是软件系统资源的占用情况。
微信不会杀死的原因是小米系统用的是腾讯管家模块进行关系的,
所以进行内存清理的时候,自带的系统自然不会清理微信的产品了。
但是360手机卫士虽然被清理掉了,但是能自动重启,这点真的很厉害。
  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    maven如何加自定义的包
    redhat 2.6 (santigo 5.6) vsftp
    shell如何实现自动填写操作执行下
    linux shell 文件配置sh:color:
    shell script语法一定要加path吗
    SecureCRT如何访问虚拟机vmWare中
    C#如何读取WINDOWS的放大系数
    cximge的图如何存入数据库并提取
    DBGRID控件显示查询结果文本类型
    TChart控件如何把表中右边的系列
    maven如何加自定义的包
    redhat 2.6 (santigo 5.6) vsftp
    shell如何实现自动填写操作执行下
    linux shell 文件配置sh:color:
    shell script语法一定要加path吗
    SecureCRT如何访问虚拟机vmWare中
    C#如何读取WINDOWS的放大系数
    cximge的图如何存入数据库并提取
    DBGRID控件显示查询结果文本类型
    TChart控件如何把表中右边的系列
    android如何在一个activity中
    android中给Dialog设置动画自
    订单签名错误,请检查PARTNER
    C#给android通过Socket发送图
    如何获取drawable文件夹下图
    service监听推送点通知栏无法
    android如何移植webkit内核
    QT中readyread()信号接收不到
    QT中readyread()信号接收不到
    error opening trace file: 
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |