How to create a service in Flutter to make an app to run always in background?

前端 未结 3 880
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 02:45

I would like to make my Flutter application to run always in background. With android, we have to create a Service that runs always in background. I don\'t find something ab

相关标签:
3条回答
  • 2020-12-01 03:38

    This is a library to do just that background_fetch.

    Background Fetch is a very simple plugin which will awaken an app in the background about every 15 minutes, providing a short period of background running-time. This plugin will execute your provided callbackFn whenever a background-fetch event occurs. ref

    0 讨论(0)
  • 2020-12-01 03:47

    There isn't a way to do this directly from flutter right now although that may change at some point - see this bug/feature request. You do have a couple of options though.

    The first is to use MethodChannels and simply write the android code you want to create a background service (or if you want it to always be a background service you can probably do that without needing communication from the flutter side).

    The second is some combination of these two plugins - android_alarm_manager and android_intent. But that won't help for all use-cases.

    0 讨论(0)
  • 2020-12-01 03:47
    • create new Flutter project.
      • create BroadcastReceiver class beside MainActivity in android dir.
      • change MainActivity.java and main.dart and AndroidManifest.xml like this

    MyReceiver

        package com.example.flutter_broadcastreceiver_alarmmanager_repeat;
        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        public class MyReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                MainActivity.callFlutter();
            }
        }
    

    MainActivity

    package com.example.flutter_broadcastreceiver_alarmmanager_repeat;
    
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.os.Bundle;
    import io.flutter.app.FlutterActivity;
    import io.flutter.plugin.common.MethodChannel;
    import io.flutter.plugins.GeneratedPluginRegistrant;
    import io.flutter.view.FlutterView;
    
    public class MainActivity extends FlutterActivity {
    
        private PendingIntent pendingIntent;
        private AlarmManager alarmManager;
        private static  FlutterView flutterView;
        private static final String CHANNEL = "com.tarazgroup";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            flutterView=getFlutterView();
            GeneratedPluginRegistrant.registerWith(this);
    
            Intent intent = new Intent(this, MyReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(this, 1019662, intent, 0);
            alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, pendingIntent);
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            alarmManager.cancel(pendingIntent);
        }
    
        static void callFlutter(){
            MethodChannel methodChannel=new MethodChannel(flutterView, CHANNEL);
            methodChannel.invokeMethod("I say hello every minute!!","");
        }
    }
    

    main.dart

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key}) : super(key: key);
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
    
      static const methodChannel = const MethodChannel('com.tarazgroup');
    
      _MyHomePageState() {
        methodChannel.setMethodCallHandler((call) {
          print(call.method);
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
          ),
          body: Container() 
        );
      }
    }
    

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.flutter_broadcastreceiver_alarmmanager_repeat">
    
        <application
            android:name="io.flutter.app.FlutterApplication"
            android:icon="@mipmap/ic_launcher"
            android:label="flutter_broadcastreceiver_alarmmanager_repeat">
            <activity
                android:name=".MainActivity"
                android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
                android:hardwareAccelerated="true"
                android:launchMode="singleTop"
                android:theme="@style/LaunchTheme"
                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:name=".MyReceiver"></receiver>
    
        </application>
    
    </manifest>
    

    your flutter code calls avery 1 min. even your app minimized or switch to another app or screen off.

    0 讨论(0)
提交回复
热议问题