gpt4 book ai didi

android - 基于 Android 示例的 Flutter 应用程序在 BOOT_COMPLETED 上自动启动不起作用

转载 作者:IT王子 更新时间:2023-10-29 06:33:38 28 4
gpt4 key购买 nike

有很多使用 BOOT_COMPLETED 在设备启动时启动应用程序的示例。我试图将这些示例用于我的 Flutter 应用程序。让它启动应用程序。这是一个显示图像的简单标牌应用程序。基本上类似于相框。

在下面的示例代码中,应用程序正在编译,但是,例如,当我重新启动模拟器时,代码似乎没有任何效果。

我的猜测是我没有调用正确的代码来实际启动应用程序。我不是 Android 开发人员,所以我无法确定到底发生了什么。 list 如下..

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="au.net.digitall.cmplayer">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:name="io.flutter.app.FlutterApplication"
android:label="cm_player"
android:icon="@mipmap/ic_launcher"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/cmTheme2"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">

<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<receiver
android:enabled="true"
android:name=".StartCmPlayerServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>

然后是StartCmPlayerServiceAtBootReceiver类启动APP..

package au.net.digitall.cmplayer;

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


public class StartCmPlayerServiceAtBootReceiver extends BroadcastReceiver {

private static final String TAG = StartCmPlayerServiceAtBootReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "BOOT detected");
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MainActivity.class);
context.startService(serviceIntent);
}
}
}

这一切都编译并运行,但重启时什么也没有发生。感谢帮助..

最佳答案

非常感谢 Mike M。他的建议和指向其他基于 android 的讨论给了我足够的信息来存档启动时的自动启动。上面示例的代码更改如下..

在 StartCmPlayerServiceAtBootReceiver 类中,改为

public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent mIntent = new Intent(context, MainActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}

再次感谢,我希望其他 flutter 开发者觉得这很有用。

关于android - 基于 Android 示例的 Flutter 应用程序在 BOOT_COMPLETED 上自动启动不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53715839/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com