I am trying to auto start an app after rebooting the phone. After looking into a few tutorials, none of the codes starts the app after rebooting the phone, and I have tested if StartActivityOnBootReceiver.java
have received the action. This is my code
AndroidManifest.xml
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
>
<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/Theme.HelloWorld">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".StartActivityOnBootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
StartActivityOnBoostReceiver.java
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);
Log.d("Receiver", "Received");
}
}
}
At first, I thought it is due to StartActivityOnBootReceiver haven't gotten the action of Intent.ACTION_BOOT_COMPLETED
, so I added Log.d("Receiver", "Received");
And this is the result of the logcat
So, StartActivityOnBootReceiver
have received the action, but it didn't turn on the app. Is there something I have done wrong?
With latest version of androids, I think this is not possible, unless you have root access or maybe even mark your app as a necessary service (some default apps are marked so by the company), i don't think it's possible normally