简体   繁体   English

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

[英]Can I receive a share intent without opening my Flutter app?

I'm creating an app in Flutter to store any type of media, imagem, video, pdfs, etc. And I want to be able to receive share intents from other apps in the easiest way possible for the user.我正在 Flutter 中创建一个应用程序来存储任何类型的媒体、图像、视频、pdf 等。我希望能够以最简单的方式为用户接收来自其他应用程序的共享意图。

So, my idea is to be able to simply receive the media without needing to open the app for the user to input something, they should simply select my app to receive the media and continue using the "source" app.因此,我的想法是能够简单地接收媒体而无需打开应用程序让用户输入内容,他们应该简单地 select 我的应用程序来接收媒体并继续使用“源”应用程序。 Is that possible in flutter? flutter 有可能吗?

this should work very well based on your requirement, receive_sharing_intent , just following the setup for android & ios and try the example:根据您的要求, 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 ?? "")
            ],
          ),
        ),
      ),
    );
  }
}

Using Receive Sharing intent package, you can receive text & media files in closed as well as opened application.使用接收共享意图package,您可以在关闭和打开的应用程序中接收文本和媒体文件。

Below code snippet to receive intent in closed application,下面的代码片段在封闭的应用程序中接收意图,

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

You can go through this link for further understanding on how to receive intent in already opened & closed application.您可以通过此链接go 进一步了解如何在已打开和关闭的应用程序中接收意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter - 接收分享意图,与 iOS 分享 PDF - Flutter - receive sharing intent, share PDF with iOS 如何从我的 flutter 应用程序分享音乐? - How can i make share music from my flutter app? 无法将我的 flutter 应用程序推送到 Playstore,因为“意图过滤器,但没有 'android:exported' 属性集”错误 - can't push my flutter app to playstore becuase of "intent filter, but without the 'android:exported' property set" error Flutter 从 Android 应用程序接收 Intent 结果 - Flutter receive Intent results from Android app 有没有一种方法可以在不上传到商店的情况下更新我的 Flutter 应用程序? - Is there a way that I can update my Flutter app without uploading to stores? 如何在不使用 App Store 的情况下将我的 Flutter 应用程序下载到我的 iPhone? - how can I download my flutter app to my iPhone without using the App Store? 颤抖分享图像意图 - Flutter share image intent Flutter 如何在共享首选项中保存我的日期 - Flutter How can i save my date in Share preferences 我可以在不更改后端功能的情况下更新 flutter 应用程序的 UI 吗? - Can I update my UI on flutter app without changing the backend functions? 如何在不移动其他应用程序的情况下使用意图共享 - How to use intent share without moving another app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM