0

I already read many topics about this theme. Even though I did almost all of them I couldn't succeed.

What I expect is when android booting is completed BootReceiver.class receives BOOT_COMPLETED message. And then BootReceiver.class executes MainActivity.class.

Here is my AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="land.incom">

    <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.AutoStart">
        <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=".BootReceiver"
            android:enabled="true" android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

My BootReceiver.java.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

And My MainActivity.java.

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button button = findViewById(R.id.button);

        button.setOnClickListener( view -> {
            System.exit(0);
        });
    }
}

Is there anything wrong in my code? My code is successfully compiled and executed well. But when my android phone is rebooted it seems that my app is not executed.

I did a program on Android Studio 4.2.1 and using SDK Android API level 30.

Thanks for your paying attention in advance.

Yavor Mitev
  • 1,363
  • 1
  • 10
  • 22
nkdpop
  • 1
  • 1

2 Answers2

0

I think I found a solution from the topic linked below.

boot_completed not working on Android 10 Q API level 29

I'll post more details after checking a few more.

nkdpop
  • 1
  • 1
0

I was able to solve this problem by doing two main things.

  1. Following the workaround written by Dan Baruch in the link below, I added the following to the AndroidManifest.mxl file.

boot_completed not working on Android 10 Q API level 29

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  1. I have allowed "Display over other apps" permission to my app.
nkdpop
  • 1
  • 1