How to prevent an application that runs in the background from being stopped by the user?

Asked

Viewed 368 times

3

For example an application that needs the strings of the apparatus every 30 seconds and that runs in the background and, the user can not force its stop manually.

3 answers

2

From what I understand you want to develop a service "zombie" that will work independent of the application. I do not advise, you have to think about battery life, even more using GPS. If it is not an internal use application of a company, you have a chance to have a high level of user rejection.

But...

For that you’ll need a Broadcastreceiver:

In the manifest:

<receiver
            android:name=".services.ReceiverCall"
            android:enabled="true"
            android:exported="false" >

            <intent-filter>
                <action android:name="SEU PACOTE.service.broadcast" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

        </receiver>

The broadcast:

public class Receivercall extends Broadcastreceiver { public receivercall() { }

    @Override
    public void onReceive(Context context, Intent intent) {
            context.startService(new Intent(context, SEU_SERVICE.class));
    }
}

1

The way would be to create a kind of "service run period" with an Alarmmanager and Schedule. There is no way to prevent the user from stopping a service, as there are several ways to stop a service. You better work on how to re-run your service.

With the onStartCommand(), the service returns an integer that sets the reset behavior, in case the service is terminated by the Android platform.

With these constants you can "control" your service processes and return your standards:

  • Service.START_STICKY
  • Service.START_NOT_STICKY
  • Service.START_REDELIVER_INTENT

READ MORE HERE

  • If the user stops the application is made a Kill -9 in the process and this does not give chance to execute any checkout code. And by being in the process any scheduling done at Alarmmanager by that process goes away together.

  • @Piovezan, you are wrong, since the system itself can be directed to restart the service. This is possible.

  • Looking better that question, can be wrong even. With the quoted constants can work.

  • 1

    Actually if the user does force stop (in the application manager > application > force stop) the service is not restarted nor with the constants cited, until the user starts the application again by the Launcher icon. At least that’s what it says that answer and also here. Alarms don’t come back either. I missed the reason but the behavior is as I said.

0

I actually solved my problem using Services, putting my application to have administrator access level and starting a new service when the user deletes the application in the service onDestroy method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.