Last active
April 22, 2024 19:14
-
-
Save tjunxiang92/de43de57cfcbfe3ce73671edf0d80fb6 to your computer and use it in GitHub Desktop.
Flutter: Receiving Shared Data from other Apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<activity | |
android:name=".MainActivity" | |
android:launchMode="singleTop" | |
android:theme="@style/LaunchTheme" | |
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density" | |
android:hardwareAccelerated="true" | |
android:windowSoftInputMode="adjustResize"> | |
<!-- Add This intent-filter --> | |
<intent-filter> | |
<action android:name="android.intent.action.SEND" /> | |
<category android:name="android.intent.category.DEFAULT" /> | |
<data android:mimeType="text/plain" /> | |
</intent-filter> | |
</activity> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'App Name', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
// Set up the channel | |
MethodChannel _channel = const MethodChannel("app.channel.shared.data"); | |
String dataShared = "No data"; | |
@override | |
void initState() { | |
super.initState(); | |
// Set up the event listener | |
_channel.setMethodCallHandler(_handleMethod); | |
} | |
// Receive event when Android receives shared content | |
Future<dynamic> _handleMethod(MethodCall call) async { | |
switch (call.method) { | |
case "onSharedIntent": | |
setState(() => dataShared = call.arguments.toString()); | |
return call.arguments; | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Text(dataShared); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.videonotesapp; | |
import android.os.Bundle; | |
import io.flutter.app.FlutterActivity; | |
import io.flutter.plugins.GeneratedPluginRegistrant; | |
// Add Imports | |
import android.content.Intent; | |
import java.nio.ByteBuffer; | |
import io.flutter.plugin.common.ActivityLifecycleListener; | |
import io.flutter.plugin.common.MethodCall; | |
import io.flutter.plugin.common.MethodChannel; | |
import io.flutter.plugins.GeneratedPluginRegistrant; | |
import io.flutter.plugin.common.MethodChannel.MethodCallHandler; | |
import io.flutter.plugin.common.MethodChannel.Result; | |
public class MainActivity extends FlutterActivity { | |
private String sharedText; | |
private MethodChannel channel; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
GeneratedPluginRegistrant.registerWith(this); | |
// Set up the channel | |
channel = new MethodChannel(getFlutterView(), "app.channel.shared.data"); | |
Intent intent = getIntent(); | |
handleIntent(intent); | |
} | |
// This handles the case where the app is paused and user has shared an intent | |
@Override | |
protected void onNewIntent(Intent intent) { | |
handleIntent(intent); | |
super.onNewIntent(intent); | |
} | |
// Handles the intent coming in and calls Flutter EventListener | |
protected void handleIntent(Intent intent) { | |
String action = intent.getAction(); | |
System.out.println(action); | |
String type = intent.getType(); | |
System.out.println(type); | |
if (Intent.ACTION_SEND.equals(action) && type != null) { | |
System.out.println("Action"); | |
if ("text/plain".equals(type)) { | |
channel.invokeMethod("onSharedIntent", intent.getStringExtra(Intent.EXTRA_TEXT)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment