Bekzhan
Bekzhan

Reputation: 575

How to start an app on boot completed in android?

I have tried every solution on the internet. But none of them worked for me. Could you check where I am doing wrong? Maybe it no longer works in higher Api. Here is my broadcastReceiver class :

public class ReceiverToStartAnApp extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        Toast.makeText(context, "Home Task 3 Version ", Toast.LENGTH_LONG).show();
        Intent myIntent = new Intent(context, CameraRecorderActivity.class);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(myIntent);
    }
}

Here is my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hometaskversion3">
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.HomeTaskVersion3">
        <activity
            android:name=".ui.CameraRecorderActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".service.RecorderService" />
        <receiver
            android:name=".receiver.ReceiverToStartAnApp"
            android:directBootAware="true"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.BOOT" />
                <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>
    </application>
</manifest>

Upvotes: 0

Views: 2489

Answers (2)

Unfortunately, starting from Android 10, to launch an app after reboot, you need to make sure that your app has the SYSTEM_ALERT_WINDOW permission granted.

Upvotes: 0

Rediska
Rediska

Reputation: 1470

First of all, apps that have never been started in some way do not receive system broadcasts. So you need some activity of your app to start before you receive ACTION_BOOT_COMPLETED. Second, as noted in the comments, it is no longer allowed to start an activity from a receiver unless some conditions are met. The preferred way to do it is to show a notification with a pending intent for the activity you need to start. This approach will be less intrusive for the user.

Upvotes: 1

Related Questions