Jam*_*ner 5 android startup dart flutter
有很多使用 BOOT_COMPLETED 在设备启动时启动应用程序的示例。我尝试将这些示例用于我的 Flutter 应用程序。让它启动应用程序。这是用于显示图像的简单标牌应用程序。基本上类似于相框。
在下面的示例代码中,应用程序正在编译,但是,例如,当我重新启动模拟器时,代码似乎没有任何效果。
我的猜测是我没有调用正确的代码来实际启动应用程序。清单如下..
<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>
Run Code Online (Sandbox Code Playgroud)
然后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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这一切都编译并运行,但在重新启动时没有任何反应。求帮助。。
非常感谢 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);
}
}
Run Code Online (Sandbox Code Playgroud)
再次感谢,我希望其他 flutter 开发者发现这很有用。
| 归档时间: |
|
| 查看次数: |
4747 次 |
| 最近记录: |