Android 10.0 Application startup on BOOT

被刻印的时光 ゝ 提交于 2020-12-08 02:07:22

问题


We have an android application which we intend to start/launch during phone boot. With some code tries in Android 10, we realized that the launching of app on boot, is not possible after Android 8.0. Previously in Android 6, it was possible. Even in physical device /phone / emulator Android 10 , we gave permission in AutoStart list our application. << Objective: Any way (workaround) to launch app on boot even on latest versions , i.e. Android 8 onwards ? >>

Tries that we made in Android 10: following are 3 sections of code - AndroidManifest.xml, MyActivity.java, MyBroadcastReceiver.java

1)AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<activity android:name=".MainActivity"  android:launchMode="singleTop" android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

2)MyActivity.java

public class MainActivity extends FlutterActivity {
@java.lang.Override
protected void onCreate(android.os.Bundle savedInstanceState) { 
super.onCreate(savedInstanceState);
// "Display pop up window"
    if (!Settings.canDrawOverlays(getApplicationContext())) {
        startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION));
    }
Log.d(TAG, "-------- onCreate -------"); // this is printed
}
}

3)MyBroadcastReceiver.java

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent i = new Intent(context, MainActivity.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(i);
Log.d(TAG, "------ tried to launch MainActivity -------"); // this is printed
}
}
}

回答1:


Starting the app on boot can be annoying to the user. You can start a Foreground Service instead. The BOOT_COMPLETE intent can be in many different form depending on your device. I have tried to capture all of them here.

In your Manifest:

<receiver android:name=".receiver.BootReceiver" // YOUR RECEIVER HERE
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
            <action android:name="android.intent.action.REBOOT" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.ACTION_SHUTDOWN" />
        </intent-filter>
    </receiver>

And in the receiver:

class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        // START FOREGROUND SERVICE HERE
    }
}


来源:https://stackoverflow.com/questions/64642362/android-10-0-application-startup-on-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!