Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autostart on BOOT_COMPLETED for flutter application based on Android example not working

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 compiling, however, when I reboot a simulator, for example, the code does not appear to have any effect.

My guess is that I am not calling the right code to actually start the application.. I am not a Android developer, so am having issues 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>

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. Appreciate the help..

like image 790
James Gardiner Avatar asked Dec 11 '18 00:12

James Gardiner


1 Answers

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

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.

like image 193
James Gardiner Avatar answered Nov 03 '22 02:11

James Gardiner