How to start an Apk after boot complete ?

Started by egrojoz, May 30, 2014, 11:53:21 AM

Previous topic - Next topic

egrojoz

The solution found didn't work for me initially A20(Android 4.2.2)
Would have to build the Android image as the best solution ?
Thank you very much.

dave-at-axon

The application requires an entry in the manifest and a permission so unless you have access to the source you won't be able to make a specific app start on boot. You can create a small app that does run on boot complete and this can then start the app if you know the intent. Google how to start other apps. Tons of help on this. You can then use the following to create a boot receiver application.

You'll need this in your application manifest file

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

And this.


<receiver
            android:name="com.yourappname.MyBootReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>


I then have this code to handle the bootreceiver. I use a timer as I want a delay before it runs.


package com.yourappname;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;

public class MyBootReceiver extends BroadcastReceiver {

    Context appContext;
    private Handler mHandler = new Handler();

    public MyBootReceiver() {
    }

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

        appContext = context;

        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            Log.d("BootReceiver", "Starting MainActivity in 10 seconds");
            //
            // This handler will start the main activity in 10 seconds
            //
            mHandler.removeCallbacks(mStartSystemTimeTask);
            mHandler.postDelayed(mStartSystemTimeTask, 10000); // Wait 10 seconds
        }
    }

    //*******************************************************************
    // This handler starts the main activity as if someone had clicked
    // on the applications icon
    //*******************************************************************

    private Runnable mStartSystemTimeTask = new Runnable() {
        public void run() {
            try {
                mHandler.removeCallbacks(mStartSystemTimeTask);
                mHandler = null;

                Intent mainActivity = new Intent(appContext, MainActivity.class);

                mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                appContext.startActivity(mainActivity);

            } catch (Exception error) {
                Log.d("BootReceiver", "Unable to start MainActivity");
            }
        }
    };
}


Intent.FLAG_ACTIVITY_NEW_TASK is important or your activity won't start.


This is code for your own custom app you want to start on boot complete. You just need to change the code in the mainActivity bit above to handle starting your external application.