Home > Net >  Auto restart or power off/on mode on my mobile device, open the Application automatically in React n
Auto restart or power off/on mode on my mobile device, open the Application automatically in React n

Time:11-26

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

CodePudding user response:

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.

  • Related