android如何做到类似于微信那样后台服务不会被杀死?

失落夏天 版主
Android领域优质创作者
博客专家认证
2014-02-11 11:05:42
正在做一款锁屏应用。
做锁屏肯定用到了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();
}
}
...全文
44375 54 打赏 收藏 转发到动态 举报
写回复
用AI写文章
54 条回复
切换为时间正序
请发表友善的回复…
发表回复
信息就是力量 2016-10-28
  • 打赏
  • 举报
回复
明明就是两个服务,安卓手机最主要的一个问题就是可以由另一个程序启动,这样只要你装了腾讯的其它软件它就会监听到微信有没有被杀死,其实后台服务能不能被杀死是由系统决定的,我华为手机就可以很轻松的把微信杀死,但三星的就不行,因为华为的系统锁屏后只要没有被手动授权后台运行的服务都会被杀死,这样可以节约电能,如果你的手机做不到,只能说你的手机系统没有优化好。但如果你想写一个不被杀死的软件,估计也没有人敢用你的,现在很多手机系统都对这些软件直接进行清理,还会提示用户这个软件太浪费电了应该卸载掉,在安卓6.0以后很多这样的软件都会因为权限问题跑出BUG,因为它们在后台要做的事情如果没有被授权就会出现异常,不像之前的系统可以由程序主动授权!还是早点还安卓系统一片绿色天空吧,不要搞这种流氓软件了,一个服务就能搞定的事情搞两个服务除了多占用点内存外并没有什么好处,还会导致程序逻辑复杂化不能专注于处理好本身应用应该做到的一些事,像微信的内存就没有优化好,没有任务消息处理时还占了一百多M的内存,这就是明显的只管走自己的跑,把用户当傻子,其实很多用户也都关心内存和性能的,我一般设置黑屏就直接把微信给清掉。
奋斗年轻人 2016-09-19
  • 打赏
  • 举报
回复
同求,1142194897@qq.com
nishihaorenwoyeshi 2016-09-08
  • 打赏
  • 举报
回复
楼主给份源码吧 lys221221@163.com
Over_wang 2016-08-26
  • 打赏
  • 举报
回复
这方法名起的,我看的头疼 谁要是接这个代码习惯的活 估计头大吧
chunlv123 2016-06-29
  • 打赏
  • 举报
回复
引用 30 楼 Aries_zz 的回复:
[quote=引用 19 楼 AA5279AA 的回复:] 其实这个问题早就解决了,只不过忘了回来解释一下了。。 微信杀不掉的原因并不是它本身用了什么特殊的技术,只是因为和小米系统签订了合同,所以不会被杀掉,有兴趣的人可以做个试验,做个工程和微信一样的主包名,然后服务名也一样的话,看看是否会被杀掉。 想解决这方式,两种方法 第一,引导用户赋予应用自启动的权限,并且修改 service方法中 onStartCommand的flags=1,这样即使被杀掉,也会在被杀掉后重新自启动的。 第二,把应用刷成系统级的应用。
对你说的这两点我说一下,首先,微信不是系统级应用,否则是不能删掉的。 然后,非小米系统一样是删不掉。 你自己仔细去设置里面的后台进程观察一下就可以发现一些东西了。[/quote] 怎么引导用户赋予应用自启动权限的?我想跳转到自启动权限管理的那个页面,但不知道怎么做,能给个思路吗?谢谢
xfyunyun 2016-06-12
  • 打赏
  • 举报
回复
引用 47 楼 dovefairy 的回复:
你可以创建一个 守护线程,让两个线程相互守护,当一个线程杀死,另一个线程立即将将它重启,这样线程就不会被杀死了,当然这个方法有待你的优化
请问您是不是实现过双进程守护?能不能向您请教几个问题?qq:1257815105
momocaocao 2016-06-06
  • 打赏
  • 举报
回复
引用 47 楼 dovefairy 的回复:
你可以创建一个 守护线程,让两个线程相互守护,当一个线程杀死,另一个线程立即将将它重启,这样线程就不会被杀死了,当然这个方法有待你的优化
这个真的有用么?要是不同机子不会被杀掉么?
dovefairy 2016-05-16
  • 打赏
  • 举报
回复
你可以创建一个 守护线程,让两个线程相互守护,当一个线程杀死,另一个线程立即将将它重启,这样线程就不会被杀死了,当然这个方法有待你的优化
  • 打赏
  • 举报
回复
有人解决么?这个问题,最近也要用的
振_兴 2016-04-25
  • 打赏
  • 举报
回复
解决了吗?求好心人给个结果,咋样实现的,什么思路?
龙爷linux 2016-02-17
  • 打赏
  • 举报
回复
[yabao=黄冈][/yabao]
l900919zc 2015-11-27
  • 打赏
  • 举报
回复
其一,并不是楼主所谓的签订合同 其二,系统级应用也不现实 其三,实现方式是进程守护,通过调用native函数创建一个子进程。父子进程相互监听,若子进程死去,父进程妥善处理后重新创建新的子进程;若父进程死去,子进程使用AM命令重启父进程 可以打开你的手机设置,正在运行的服务,看看是不是两个进程
yong_815 2015-10-14
  • 打赏
  • 举报
回复
楼主还活着吗?出来详细分析下啊
brane_sh 2015-08-25
  • 打赏
  • 举报
回复
楼主 是否给一份这块的源码 邮箱 brane_sh@aliyuun.com
JUNJ6859 2015-08-21
  • 打赏
  • 举报
回复
楼主解决这个问题了吗?能给我发一份解决代码吗(913734278@qq.com)最近这个问题研究好久都没有头绪,多谢了
qeaq 2015-08-20
  • 打赏
  • 举报
回复
楼主解决这个问题了吗?能给我发一份解决代码吗(308289006@qq.com)最近这个问题研究好久都没有头绪,多谢了
qq_19266191 2015-08-06
  • 打赏
  • 举报
回复
请问有人解决这个问题了吗?求交流,我现在也在为这个事情头疼
漫步云端007 2015-05-12
  • 打赏
  • 举报
回复
这东西是跟白名单黑名单有关的。
Benny810 2015-05-03
  • 打赏
  • 举报
回复
同问楼主,问题彻底解决了吗?可否也发我一份实现代码(我的邮箱250893029@qq.com,谢谢),被这个问题折腾了好几天了,各种方法试验了都无效。
yyqao2011 2015-04-13
  • 打赏
  • 举报
回复
楼主,可以给一份具体的实现代码吗,619784760@qq.com 最近也一直在看这个,网上看了很多都没解决
加载更多回复(31)

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧