对于基于Android示例的Flutter应用程序,无法在BOOT_COMPLETED上自动启动 [英] Autostart on BOOT_COMPLETED for flutter application based on Android example not working

查看:86
本文介绍了对于基于Android示例的Flutter应用程序,无法在BOOT_COMPLETED上自动启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There are quite a few examples of using BOOT_COMPLETED to start an application when the device boots.. I have attempted to use these example against my Flutter application. Having it start the App. This is for a simply signage app that shows images. Basically similar to a picture frame.

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

In the example code below, the application is compileing, however, when I reboot a simulator, for example, the code does not appear to have any effect.

我的猜测是,我没有调用正确的代码来真正启动应用程序..我不是Android的devaloper,所以issus正在弄清楚到底是怎么回事.清单如下.

My guess is that I am not calling the right code to actually start the application.. I am not a Android devaloper, so am having issus figuring what is exactly going on. Manifest follows..

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

Then the StartCmPlayerServiceAtBootReceiver class to start the 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);
        }
    }
}

这一切都可以编译并运行,但是重新启动后什么也没有发生.申请帮助.

This all compiles and runs, but nothing happens on reboot. Appriciate the help..

推荐答案

非常感谢MikeM.他的建议和其他基于android的讨论为我提供了足够的信息,可以在启动时存档自动启动.代码更改为上面的示例..

Thank to very much to Mike M. His suggestion and pointing at the other android based discussion gave me enough info to archive autostart on boot. The code change to the above example follows..

在StartCmPlayerServiceAtBootReceiver类中,更改为

In the StartCmPlayerServiceAtBootReceiver class, Change to

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

再次感谢,希望其他开发者也能从中受益.

Thanks again, and I hope other flutter devs find this useful.

这篇关于对于基于Android示例的Flutter应用程序,无法在BOOT_COMPLETED上自动启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆