John Okpo
John Okpo

Reputation: 21

App Start on Boot Complete Not working for my Kotlin app

Help, I posted here some days back, luck for me someone guided me to a solution, on how I could get my app started on boot_completed, this tutorial was straightforward and explanatory, but my app is not starting on my Infinix Smart 5, even after following the lectures carefully, please help. Below are my codes.

Manifest

<?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="com.example.jofi">
<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.Jofi">
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
   <!-- For MainActivity --!>
   <!-- Ends here --!>
  <receiver
          android:name=".MyReceiverUpOnBoot"
          android:enabled="true"
          android:exported="true">
          <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED"/>

              <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
          </intent-filter>
      </receiver>
 </application>
</manifest>

MyReceiverUpOnBoot.kt

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log

class MyReceiverUpOnBoot : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        // This method is called when the BroadcastReceiver is receiving an Intent broadcast.
        if (Intent.ACTION_BOOT_COMPLETED == intent!!.action) {
                val i = Intent(context, MainActivity::class.java)
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                context!!.startActivity(i)
                Log.i("From BootUp", "onStart")

            }
        
    }
}

Upvotes: 0

Views: 530

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006664

You cannot start an activity from the background on modern versions of Android.

If you want your UI to appear when the phone starts up, implement a launcher.

Upvotes: 1

Related Questions