Ahmed Bassiouny
Ahmed Bassiouny

Reputation: 275

Android: run always in background

i am developing a simple program that should create a notification with some data when the mobile battery is low.

the application is working fine, but I must open the app by myself at first or if the phone was switched off and on, and if the user closed the app from the currently running apps menu it will close too.

How can i make it like truecaller for example so that after the user opens it after the installation it will continue working forever ?

Upvotes: 0

Views: 976

Answers (2)

Knossos
Knossos

Reputation: 16048

You must start your monitoring service on Boot of the device for it to be available after a reboot of the device.

Example BroadcastReceiver:

<receiver android:name=".BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.HOME" />
    </intent-filter>
</receiver>

This receiver BootReceiver can then start your Service.

Your App will be needed to be ran at least once by the user manually, after installation. This is a security procedure in Android.

Upvotes: 1

dubplay
dubplay

Reputation: 83

I think http://developer.android.com/training/run-background-service/create-service.html would do you good.

The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. Also, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask

Upvotes: 0

Related Questions