【问题标题】:Android 10.0 Application startup on BOOTAndroid 10.0 应用程序开机启动
【发布时间】:2021-02-14 22:07:34
【问题描述】:

我们有一个 android 应用程序,我们打算在手机启动期间启动/启动它。 通过在 Android 10 中尝试一些代码,我们意识到在 Android 8.0 之后无法在启动时启动应用程序。以前在 Android 6 中,这是可能的。 即使在物理设备/手机/模拟器 Android 10 中,我们在 AutoStart 列表中授予了我们的应用程序的权限。 >

我们在 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>

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
}
}
}

【问题讨论】:

  • @Dan:感谢您的建议。我编辑了代码:(​​a) 在 MyBrodcastReceiver.java :: onReceive() 中添加了清单文件 SYSTEM_ALERT_WINDOW (b) -- 我编辑了 -->> 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, "----- 试图启动 MainActivity --------"); // 这打印在日志中...但应用程序没有启动..
  • 你能显示代码吗?编辑您的第一篇文章以包含更改以将其签出
  • @Dan :我在上面的帖子中编辑了代码。我在我的 Redmi 6 pro MIUI Global 11.0.7.1、Android 9 PKQ1.180917.001 中测试/部署该应用程序。我什至在模拟器 Pixel 2 x86 Android 10.0+ 中尝试/测试过。同样的结果,启动后,应用程序无法启动。在 Redmi 6 pro 中,应用程序甚至被列入 AutoStart 权限列表。
  • 您是否检查过覆盖权限?您是否确保已将它们提供给您的应用?

标签: android boot launch


【解决方案1】:

在启动时启动应用程序可能会让用户感到厌烦。您可以改用Foreground Service。 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>

在接收器中:

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

【讨论】:

  • 基本上,通过在onReceive()中启动期间的服务启动,完成并测试。启动完成后,我们在通知抽屉中有一个通知。当用户点击该通知时,我们正在启动应用程序(这是通过 PendingIndent() 完成的)。这没关系并且有效。但我们的目标/要求是——另外,在 Android 8+ 中,我们需要在启动时启动应用程序,而不需要点击通知手动启动。
猜你喜欢
  • 2012-02-09
  • 1970-01-01
  • 2013-03-10
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多