views:

89

answers:

2

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

A: 

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.

hackbod
I am looking into being killed by user/task killers. I am perfectly ok with the system killing the application for memory. Once again this is proof of concept, I am trying to see if such an app can be built for security reasons. I understand such behavior is "treading dangerously close to malware". The main question would be how to correctly bind to a service and have the onServiceDisconnected method called when the service is killed.
Murdock
As of 2.2 task killers can not kill services. Prior to that, there is not really anything you can do.
hackbod
A: 

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.

schwiz
The onDestroyed method only gets called when the operating system is shutting down the service so the application can do things like persist information. I am looking to get around the user or another application killing the service which does not trigger the onDestroy method.
Murdock