我可以在不打开Flutter应用的情况下接收共享Intent吗?

hl0ma9xz  于 7个月前  发布在  Flutter
关注(0)|答案(3)|浏览(146)

我正在Flutter中创建一个应用程序来存储任何类型的媒体,图像,视频,PDF等,我希望能够以最简单的方式为用户接收来自其他应用程序的共享意图。
所以,我的想法是能够简单地接收媒体,而不需要打开应用程序的用户输入的东西,他们应该简单地选择我的应用程序接收媒体,并继续使用“源”应用程序。这是可能的Flutter?

q3aa0525

q3aa05251#

这应该很好地工作基于您的要求,receive_sharing_intent,只是下面的设置为Android & iOS和尝试的例子:

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:receive_sharing_intent/receive_sharing_intent.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  StreamSubscription _intentDataStreamSubscription;
  List<SharedMediaFile> _sharedFiles;
  String _sharedText;

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

    // For sharing images coming from outside the app while the app is in the memory
    _intentDataStreamSubscription =
        ReceiveSharingIntent.getMediaStream().listen((List<SharedMediaFile> value) {
      setState(() {
        print("Shared:" + (_sharedFiles?.map((f)=> f.path)?.join(",") ?? ""));
        _sharedFiles = value;
      });
    }, onError: (err) {
      print("getIntentDataStream error: $err");
    });

    // For sharing images coming from outside the app while the app is closed
    ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
      setState(() {
        _sharedFiles = value;
      });
    });

    // For sharing or opening urls/text coming from outside the app while the app is in the memory
    _intentDataStreamSubscription =
        ReceiveSharingIntent.getTextStream().listen((String value) {
      setState(() {
        _sharedText = value;
      });
    }, onError: (err) {
      print("getLinkStream error: $err");
    });

    // For sharing or opening urls/text coming from outside the app while the app is closed
    ReceiveSharingIntent.getInitialText().then((String value) {
      setState(() {
        _sharedText = value;
      });
    });
  }

  @override
  void dispose() {
    _intentDataStreamSubscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    const textStyleBold = const TextStyle(fontWeight: FontWeight.bold);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              Text("Shared files:", style: textStyleBold),
              Text(_sharedFiles?.map((f)=> f.path)?.join(",") ?? ""),
              SizedBox(height: 100),
              Text("Shared urls/text:", style: textStyleBold),
              Text(_sharedText ?? "")
            ],
          ),
        ),
      ),
    );
  }
}

字符串

ylamdve6

ylamdve62#

使用Receive Sharing intent包,您可以在关闭和打开的应用程序中接收文本和媒体文件。
下面的代码片段接收关闭的应用程序中的意图,

// For sharing images coming from outside the app while the app is closed
    ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
      setState(() {
        _sharedFiles = value;
      });
    });

字符串
您可以通过此link进一步了解如何在已打开和关闭的应用程序中接收意图。

oewdyzsn

oewdyzsn3#

未更新“receive_sharing_intent”包,您可以改用share_handler

相关问题