0

I have an app that automatically starts a few seconds delayed after boot. BTW: This is nothing evil! The user must enable it by himself! When I now tap on the app's icon I would like to have the same instance being opened that was already started at boot. I don't want the app to restart. But this is what happens right now.

Background: I'm writing some debug info to a TextView. When the app starts at boot, it is immediately put to background with moveTaskToBack(true), because it should not cover the screen of course. The user should see the IDLE screen. So the app (and in turn this output) can only become visible when the app is opened manually.

Here is the AndroidManifest.xml:

<application android:icon="@drawable/fritzbox"
             android:label="@string/app_name"
             android:allowTaskReparenting="true"
             android:persistent="true">

    <uses-library android:name = "com.google.android.maps"              />

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

    <activity android:name =".WiFiOnDemandActivity"
              android:label="@string/app_name"
              android:allowTaskReparenting="true"
              android:launchMode="singleInstance">
        <intent-filter>
            <action   android:name = "android.intent.action.MAIN"       />
            <category android:name = "android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<uses-permission android:name="android.permission.INTERNET"               />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"   />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE"                />
<uses-permission android:name="android.permission.READ_CONTACTS"          />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"   />
<uses-permission android:name="android.permission.WAKE_LOCK"              />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

As you can see I've already tried android:launchMode, android:allowTaskReparenting and android:persistent. The broadcast receiver that receives the BOOT_COMPLETED broadcast creates a PendingIntent that is scheduled with the AlarmManager. So the app is started 15s (START_DELAY) after boot. Here is the code:

public class BootUpReceiver extends BroadcastReceiver implements Defines
{       
    private AlarmManager  mAlarmManager  = null;
    private Intent        mIntent        = null;
    private PendingIntent mPendingIntent = null;


    @Override
    public void onReceive( final Context context, final Intent intent)
    {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE   );

        mIntent       = new Intent ( context, WiFiOnDemandActivity.class );         
        mIntent.setAction(intent.getAction());
        mIntent.addFlags ( Intent.FLAG_ACTIVITY_NEW_TASK );

        mPendingIntent       = PendingIntent.getActivity( context, 0, mIntent, 0);

        mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + START_DELAY, mPendingIntent );
    }
}

Strangely enough sometimes it seems to work as expected (no restart) but sometimes it doesn't!

Thanks!

Bernd

Bernd
  • 675
  • 2
  • 8
  • 30
  • It is all a bit vague at the moment. How are you starting this app right now? what kind of intent flags are you using for example? Could you paste some code to make clear what exactly you are doing and where it goes wrong? – Wottah May 08 '12 at 07:15
  • I have updated my question. Please see the initial posting above. Thanks! – Bernd May 08 '12 at 07:57

1 Answers1

0

Do show your code, but this cannot be guaranteed. The system may kill your process at any time if memory is low, so you might get a new process when you start via the launcher. What makes you think you need the exact same instance?

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • I have updated my question. Please see the initial posting above. Thanks! – Bernd May 08 '12 at 07:56
  • Write your debugging info to a file and load it when you start the activity. What you are trying to do cannot be done reliably. – Nikolay Elenkov May 08 '12 at 08:07
  • Ok, good to know that this is obviously an Android issue. Nevertheless it is astounding that such a (in my eyes) simple task cannot be achieved. – Bernd May 08 '12 at 11:19
  • It's not an 'issue', it's just the way Android works. An 'application' is not bound to a single process, and nothing lives forever. If more resources are needed, your activity or service will be killed and restarted when necessary. – Nikolay Elenkov May 08 '12 at 12:56
  • Yes you're right. I didn't mean an issue in terms of a bug. ;-) Ok, now I've solved the problem by creating/starting a service at boot time and not the app. This is probably the better solution anyway. – Bernd May 08 '12 at 17:22