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