Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to register a Flutter app as an Android Intent Filter and to handle Incoming Intents?

Tags:

flutter

dart

One can launch another Activity using an Intent from a Flutter app: 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!')
      )
    )
  ));
}

But can one do the following with the Flutter Activity Intent services when an Intent is passed to the app? http://developer.android.com/training/sharing/receive.html

. . .
void onCreate (Bundle savedInstanceState) {
    ...
    // Get intent, action and MIME type
    Intent intent = getIntent();
. . .
like image 370
Pieter Greyling Avatar asked Feb 19 '16 13:02

Pieter Greyling


People also ask

Can flutter be used for Android app development?

Flutter is an open source framework developed by Google that lets you build natively compiled, multiplatform applications from a single codebase. Flutter 3 supports six platform targets: Android, iOS, Windows, macOS, Linux, and web applications.

How do you get share intents in flutter?

You don't need your app to be running at all times to receive the data (through intent filter) and Flutter has nothing to do anything with it. Just open your AndroidManifest. xml file, add your intent-filter , there, its type and so on.


2 Answers

To my knowledge, there is no way to handle incoming Intents from Dart code at this time. Specifically the case of handling incoming URLs is tracked by https://github.com/flutter/flutter/issues/357.

It's also possible to handle incoming intents from Java code and post the result across to Dart using the HostMessage system documented at https://flutter.io/platform-services/

Update 2020- Accept incoming intent in the Flutter from Flutter Doc

like image 100
Eric Seidel Avatar answered Oct 22 '22 18:10

Eric Seidel


This maybe can help u, thios show how to handle 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>

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

And finally to get it

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

But if need to send a real intent to android system u can use this library

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

like image 3
Helio Soares Junior Avatar answered Oct 22 '22 16:10

Helio Soares Junior