0

This question is along the lines of the one I asked for iOS, and not related to this one.

I want to know what are the mechanisms that one can use to launch an app on the Android device that has been force quit, or which hasn't been started since the device was last restarted. I imagine there is no way to launch the app in the foreground, but there should be a way to launch it in the background.

Perhaps we can send some sort of "silent remote notification" which will launch our app in order to process this notification, and which may in turn decide to set a local notification to display to the user.

I am looking for a documented 100% reliable way that is not subject to the whim of the OS, but will actually launch the app in the background, and allow it to do things.

Community
  • 1
  • 1
Gregory Magarshak
  • 1,883
  • 2
  • 25
  • 35

2 Answers2

0

Try this guide. Basically you add this to your manifest:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name">
    ...
    <!--
        Because android:exported is set to "false",
        the service is only available to this app.
    -->
    <service
        android:name=".RSSPullService"
        android:exported="false"/>
    ...
<application/>

And then you add this block to whatever you need:

public class RSSPullService extends IntentService {
    @Override
    protected void onHandleIntent(Intent workIntent) {
        // Gets data from the incoming Intent
        String dataString = workIntent.getDataString();
        ...
        // Do work here, based on the contents of dataString
        ...
    }
}

This guide also helps.

Dennis Str
  • 29
  • 10
0

You can launch an app from a BroadcastReceiver in response to a wide variety of system broadcasts, including device boot, scheduled alarms, etc. The correct component for hosting background processes is a Service. Services are less likely to be killed if they request foreground status, which creates an ongoing (non-dismissible) notification.

The main caveat is that your BroadcastReceiver will never be called unless the user has manually launched one of the app's activities since it was installed, or since the last time it was force closed.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82