0

how-do-i-start-my-app-when-the-phone-starts-on-android

Based on above link I have modified my native code in React native project as given below, but I am not getting any result..!!

Androidmanifest.xml ``

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.freshot.co.ownerapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

  
    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      <meta-data  
        android:name="com.google.android.geo.API_KEY"  
        android:value="AIzaSyA88RySEu8iI-oY15iKKLE25WKVjKefKjk"/>
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        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="com.freshot.co.ownerapp.BootReciever"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <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>

``

BootReciever.java

`

package com.freshot.co.ownerapp;

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

public class BootReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction() != null) {
           if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                Intent i = new Intent(context, MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }
    }
  }
}

`

My Sollution should be : When ever Mobile phone / Tab Reboots or power off/on, My React native Application should open automatically

GS James
  • 3
  • 3

1 Answers1

2

Won't work on modern android- broadcast receivers are background tasks. Background tasks can't launch foreground activities. This method works for you to do some quick background processing, or set up some alarms for later, but it won't allow you to display an activity.

Are you a kiosk mode app where you want to take over the homescreen and lock the user into your app? If so, you should be writing a launcher app. If not, this kind of behavior is highly discouraged up to almost being active malware- popping up your app on boot isn't user friendly, its spammy and annoying.

To make an activity the home screen for a kiosk, use

   <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />

        <category android:name="android.intent.category.HOME" />

        <category android:name="android.intent.category.DEFAULT" />

    </intent-filter>

in your manifest, then set your app as the default launcher after install. After that, it will always launch your app on boot and when the user clicks the home button.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • its like kiosk mode app when ever the app is opened it will lock screens but after reboot or restart its not opening automatically – GS James Nov 25 '22 at 04:27
  • @GSJames Check out my edit. It shows you how to declare your activity to be a launcher app, so it can be the home screen of your device. – Gabe Sechan Nov 25 '22 at 07:34
  • Thank you so much sir ...! Above given code is added inside Androidmanifest after that Reboot App is asking for launch, after selecting it Always it opens automatically – GS James Nov 25 '22 at 12:11