开发者

Creating an Android Service that cannot be killed

开发者 https://www.devze.com 2023-01-15 21:36 出处:网络
I am trying to write an app that cannot be killed by a user or another application.I understand this goes against what Android has designed for its platform, this is more of a proof of concept.The pla

I am trying to write an app that cannot be killed by a user or another application. I understand this goes against what Android has designed for its platform, this is more of a proof of concept. The plan is two have two apps, app1 and app2. When app1 starts it will start app2 and then bind onto 开发者_C百科app2, when app2 starts it will make sure app1 is started if not, then start it and bind onto app1. The point of app1 binding on app2 and app2 binding on app1 is so when one of them gets killed a method will be called that the binding has been disconnected and the app can then be restarted. I currently have the apps starting each other when they start up but I cannot seem to get them to restart when I force close one of them.

For App1 service I have and app2 very similar:

    @Override
public void onCreate()
{
    this.setForeground(true);
    this.startService((new Intent()
            .setComponent(new ComponentName("com.app2",
                    "com.app2.App2Service"))));
    this.bindService((new Intent()
            .setComponent(new ComponentName("com.app2",
                    "com.app2.App2Service"))), mConnection,
            BIND_DEBUG_UNBIND);

And for my class that implements ServiceConnection I have:

    @Override
public void onServiceDisconnected(ComponentName name)
{
    Log.d(TAG, "onServiceDisconnected:" + name.getClassName());
    mContext.startService((new Intent()
            .setComponent(new ComponentName("com.app2",
                    "com.app2.App2Service"))));
}

I think the issue I am having is that onServiceDisconnected never gets called when app1 or app2 is killed. I think this may be because I am not correctly binding to a service.

W/JavaBinder(  884): ibinderForJavaObject: 0x436274a0 is not a Binder object
W/JavaBinder(  891): ibinderForJavaObject: 0x43621d30 is not a Binder object

So my question would be, how can I bind services together so when one gets force closed that I will get a notice on the other?


You first need to specify "can not be killed". By the user? By the platform? By a task killer?

I doubt there is anything you can do with two .apks to generally make you less likely to be killed, besides obscuring to the user what you are doing. (And if your goal is to obscure to the user... this is treading dangerously close to malware.)

The correct way to tell the platform to not kill your app is with Service.startForeground(). This says "the user is aware of my service so it would be really nice for it to keep running." To go along with that, a notification is posted to ensure the user is aware of it and knows how to get rid of it.

Other than that, the platform has become increasingly aggressive about killing apps running in the background. (Though of course with services that have requested so, they can be automatically restarted.) This has been one of the causes of bad performance on devices, as apps abuse running in the background and cause the overall system to run low on resources. Thus if there are ways to work around the current semantics, you can expect those to be closed in future versions of the platform.


just try to anwser..

Service cannot be killed. in the onDestroy method, start the service again.. ^^


my thought would be to just send out a broadcast in the onDestroy method of your service so the other service can know it was destroyed.


In the official documentation I read that the method "onServiceDisconnected()" gets called only when your are trying binding to a service and for some reason you can't. So your connection gets closed. That's doesn't mean that this method catch unbinding from the client side. You should follow @Adekdik and use the method "onDestroy()" instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消