Android 10.0 应用程序在 BOOT 上启动

shi*_*rty 7 boot android launch

我们有一个 Android 应用程序,我们打算在手机启动期间启动/启动。通过在 Android 10 中尝试一些代码,我们意识到在 Android 8.0 之后无法在启动时启动应用程序。以前在 Android 6 中,这是可能的。即使在物理设备/手机/模拟器 Android 10 中,我们也在 AutoStart 列表中授予了我们的应用程序权限。<<目标:有什么方法(解决方法)可以在启动时启动应用程序,即使是在最新版本(即 Android 8 及以上版本)上?>>

我们在 Android 10 中所做的尝试:以下是 3 段代码 - 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>
Run Code Online (Sandbox Code Playgroud)

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
}
}
Run Code Online (Sandbox Code Playgroud)

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
}
}
}
Run Code Online (Sandbox Code Playgroud)

Ati*_*sal 3

在启动时启动应用程序可能会让用户感到厌烦。您可以启动前台服务。BOOT_COMPLETE 意图可以有多种不同的形式,具体取决于您的设备。我试图在这里捕捉到他们所有人。

在你的清单中:

<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>
Run Code Online (Sandbox Code Playgroud)

在接收器中:

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

  • 基本上,通过在 onReceive() 中启动期间启动服务来完成并测试。启动完成后,我们会在通知抽屉中收到一条通知。当用户点击该通知时,我们将启动应用程序(这是通过 PendingIndent() 完成的)。这没问题并且可以工作。但我们的目标/要求是 - 另外,在 Android 8+ 中,我们需要在启动期间启动应用程序,而不是需要点击通知来手动启动。 (3认同)