是否可以将Flutter应用程序注册为Android Intent Filter并处理Incoming Intents?

问题描述 投票:17回答:2

可以使用Flutter应用程序中的Intent启动另一个Activity:https://github.com/flutter/flutter/blob/master/examples/widgets/launch_url.dart

import 'package:flutter/widgets.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(new GestureDetector(
    onTap: () {
      Intent intent = new Intent()
        ..action = 'android.intent.action.VIEW'
        ..url = 'http://flutter.io/';
      activity.startActivity(intent);
    },
    child: new Container(
      decoration: const BoxDecoration(
        backgroundColor: const Color(0xFF006600)
      ),
      child: new Center(
        child: new Text('Tap to launch a URL!')
      )
    )
  ));
}

但是当Intent传递给应用程序时,可以使用Flutter Activity Intent服务执行以下操作吗? http://developer.android.com/training/sharing/receive.html

. . .
void onCreate (Bundle savedInstanceState) {
    ...
    // Get intent, action and MIME type
    Intent intent = getIntent();
. . .
dart flutter
2个回答
9
投票

据我所知,目前无法从Dart代码处理传入的Intent。具体来说,https://github.com/flutter/flutter/issues/357会跟踪处理传入URL的情况。

它还可以处理来自Java代码的传入意图,并使用https://flutter.io/platform-services/中记录的HostMessage系统将结果发布到Dart


2
投票

这可能有助于你,thios展示如何处理https://flutter.io/flutter-for-android/#what-is-the-equivalent-of-an-intent-in-flutter

<activity
  android:name=".MainActivity"
  android:launchMode="singleTop"
  android:theme="@style/LaunchTheme"
  android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
  android:hardwareAccelerated="true"
  android:windowSoftInputMode="adjustResize">
  <!-- ... -->
  <intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
  </intent-filter>
</activity>

关于MainActivity

public class MainActivity extends FlutterActivity {

  private String sharedText;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
      if ("text/plain".equals(type)) {
        handleSendText(intent); // Handle text being sent
      }
    }

    MethodChannel(getFlutterView(), "app.channel.shared.data")
      .setMethodCallHandler(MethodChannel.MethodCallHandler() {
        @Override
        public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
          if (methodCall.method.contentEquals("getSharedText")) {
            result.success(sharedText);
            sharedText = null;
          }
        }
      });
  }

  void handleSendText(Intent intent) {
    sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
  }
}

最后得到它

class _SampleAppPageState extends State<SampleAppPage> {
  static const platform = const MethodChannel('app.channel.shared.data');
  String dataShared = "No data";

  @override
  void initState() {
    super.initState();
    getSharedText();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Center(child: Text(dataShared)));
  }

  getSharedText() async {
    var sharedData = await platform.invokeMethod("getSharedText");
    if (sharedData != null) {
      setState(() {
        dataShared = sharedData;
      });
    }
  }
}

但如果需要向android系统发送真实意图,你可以使用这个库

https://github.com/flutter/plugins/tree/master/packages/android_intent

© www.soinside.com 2019 - 2024. All rights reserved.