android - 基于 Android 示例的颤振应用程序在 BOOT_COMPLETED 上自动启动不起作用
问题描述
有很多使用 BOOT_COMPLETED 在设备启动时启动应用程序的示例。我尝试将这些示例用于我的 Flutter 应用程序。让它启动应用程序。这是一个简单的显示图像的标牌应用程序。基本上类似于相框。
在下面的示例代码中,应用程序正在编译,但是,例如,当我重新启动模拟器时,代码似乎没有任何效果。
我的猜测是我没有调用正确的代码来实际启动应用程序。我不是 Android 开发人员,所以在弄清楚到底发生了什么时遇到了问题。清单如下..
<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 - 如何使用 LeanBack android tv 添加空片段
- cygwin - Linux - dd 命令失败并出现错误 [设备上没有剩余空间]
- java - Android 上的心率变化
- kotlin - 只能在协程体错误内调用挂起函数
- c# - 如何制作带有标签的向上计数计时器
- dependency-injection - StateSubscriber.Subscribe 未处置
- php - 使用 PDO 在 PHP 上未定义的变量
- xamarin - 如何获取 Lottie 动画的当前帧?
- java - 有没有办法在jpa spring boot中按列获取数据
- python - 在 Apache Nifi 中使用 ExecuteScript 不起作用