如何在Flutter中创建服务以使应用程序始终在后台运行?

时间:2018-04-30 19:19:37

标签: service dart flutter

我想让我的Flutter应用程序始终在后台运行。使用android,我们必须创建一个始终在后台运行的服务。我在Flutter文档中找不到有关服务的内容。

有可能用Flutter做这种事吗?

4 个答案:

答案 0 :(得分:4)

目前无法直接从flutter 直接执行此操作虽然可能会在某些时候发生变化 - 请参阅this bug/feature request。你确实有几个选择。

第一个是使用MethodChannels并简单地编写你想要创建后台服务的android代码(或者如果你希望它始终是一个后台服务,你可以做到这一点,而不需要从颤振端进行通信)。

第二个是这两个插件的组合 - android_alarm_managerandroid_intent。但这对所有用例都无济于事。

答案 1 :(得分:4)

这是一个仅用于background_fetch的库。

Background Fetch是一个非常简单的插件,它将每隔15分钟在后台唤醒一个应用程序,从而提供较短的后台运行时间。每当发生后台获取事件时,此插件就会执行您提供的callbackFn。 ref

答案 2 :(得分:0)

  • 创建一个新的Flutter项目。
    • 在android目录中的BroadcastReceiver旁边创建MainActivity类。
    • 像这样更改MainActivity.javamain.dartAndroidManifest.xml
  

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>
  

您的振颤代码平均每1分钟呼叫一次。甚至您的应用程序最小化或切换到另一个应用程序或关闭屏幕。

答案 3 :(得分:-1)

flutter_background_service 与来自 Timerdart:async 结合。

见: