Search code examples
javaandroidbroadcastreceiver

delay of application auto-boot


I want to delay auto start of application when device has turned on. I've tried to make this by using Handler, Timer, and AlarmManager, but nothing happend.Default start of application at ACTION_BOOT_COMPLETED working fine, but when I try to using delay, my app doesn't auto booting at all. I think it depends of BroadcastReciever's lifecycle, but I can't find detailed responce. If you have any suggestions, please leave it.There is my code:

BroacastReciever.java:

package com.example.micka.iportapp;

public class ActivityRunOnStartup extends BroadcastReceiver {

public SharedPreferences sPref;
private final int DEFAULT_APP_BOOT_DELAY = 100000;
private final boolean AUTO_BOOT_APP_FLAG = false;
public static SharedPreferences.Editor editor;
private int ACTUAL_BOOT_DELAY;




@Override
public void onReceive(final Context context, Intent intent) {

    sPref = context.getSharedPreferences("appPref",Context.MODE_PRIVATE);
    ACTUAL_BOOT_DELAY = sPref.getInt("sPref_delay",DEFAULT_APP_BOOT_DELAY);

    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        final Intent i = new Intent(context, BroadcastActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),PendingIntent.FLAG_ONE_SHOT,i,0);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME,System.currentTimeMillis()+10000,pendingIntent);

               // context.startActivity(i);





                // Actions to do after 10 seconds

            /*}
        }, 5000);*/

    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.micka.iportapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver
        android:enabled="true"
        android:exported="true"
        android:name=".ActivityRunOnStartup"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

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

    </receiver>
    <activity android:name=".BroadcastActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

Also I've tried this version:

public class ActivityRunOnStartup extends BroadcastReceiver {

public SharedPreferences sPref;
private final int DEFAULT_APP_BOOT_DELAY = 100000;
private final boolean AUTO_BOOT_APP_FLAG = false;
public static SharedPreferences.Editor editor;
private int ACTUAL_BOOT_DELAY;




@Override
public void onReceive(final Context context, Intent intent) {

    sPref = context.getSharedPreferences("appPref",Context.MODE_PRIVATE);
    ACTUAL_BOOT_DELAY = sPref.getInt("sPref_delay",DEFAULT_APP_BOOT_DELAY);

    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        final Intent i = new Intent(context, BroadcastActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                context.startActivity(i);
            }
        },10000);


    }
}

Solution

  • You're creating PendingIntent in a wrong way, to start activity you need to call PendingIntent.getActivity instead of PendingIntent.getBroadcast:

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),PendingIntent.FLAG_ONE_SHOT,i,0);
    

    should be replaced with this:

    PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),PendingIntent.FLAG_ONE_SHOT,i,0);
    

    First variant works if you want to start another BroadcastReceiver.

    Another issue is in setting alarm time, as I remember there is no need to add current time:

    alarmManager.set(AlarmManager.ELAPSED_REALTIME, 10000, pendingIntent);