user3560827
user3560827

Reputation: 694

Minimize App After Auto Launch On Boot

I have implemented an Autostart.java class which allows my app to start up after boot. I would like the app to minimize / run in background after phone starts up and only when the user clicks on the app icon will it maximize. I am using this code for auto start:

public class Autostart extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){


            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(i);
        }
    }

}

I can't implement this code without overriding the back button:

            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);

Upvotes: 1

Views: 1736

Answers (2)

user3560827
user3560827

Reputation: 694

I created an ActivityMinimizelike this:

public class ActivityMinimize extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startActivity(new Intent(ActivityMinimize.this, MainActivity.class));

    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);

}

}

I call this activity from Autostart class. works great!

Upvotes: 1

Pierre Duchemin
Pierre Duchemin

Reputation: 95

You did your broadcast receiver well, but you have to implement a service to run a background task.

Have a look to Google's offical tutorial to get started : https://developer.android.com/guide/components/services.html. You can also have a look to this post to lean more about how to launch a service after boot : Android -Starting Service at Boot Time

Upvotes: 1

Related Questions