Skip to content

Instantly share code, notes, and snippets.

@codinginflow
Created July 12, 2021 00:23
Show Gist options
  • Save codinginflow/7878602cfc5afde16d90f2d8d611c8a0 to your computer and use it in GitHub Desktop.
Save codinginflow/7878602cfc5afde16d90f2d8d611c8a0 to your computer and use it in GitHub Desktop.
Start App on Boot Tutorial
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codinginflow.onbootreceiverexample">
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".StartActivityOnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>
package com.codinginflow.onbootreceiverexample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartActivityOnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment