Flutter接收共享意图插件receive_sharing_intent_ohos的使用
Flutter接收共享意图插件receive_sharing_intent_ohos
的使用
插件介绍
receive_sharing_intent_ohos
是一个用于 Flutter 应用的插件,允许您的应用接收来自其他应用程序共享的照片、视频、文本、URL 或任何其他类型的文件。
支持情况
版本 | ohos |
---|---|
支持 | SDK 2.19.6 |
注:ohos 与 android 的字体大小不一致。
使用方法
要使用此插件,请将其添加到 pubspec.yaml
文件中作为依赖项。例如:
dependencies:
receive_sharing_intent: 1.8.0
receive_sharing_intent_ohos: 1.0.0
在模块配置文件中添加共享意图
在 receive_sharing_intent_ohos\example\ohos\entry\src\main\module.json5
文件中,添加以下内容:
...
"actions": [
"ohos.want.action.sendData"
],
"uris": [
{
"scheme": "file",
"utd": "general.entity",
"maxFileSupported": 1
},
{
"scheme": "file",
"utd": "general.object",
"maxFileSupported": 1
},
]
...
编译问题及解决方法
-
错误:No such module 'receive_sharing_intent_ohos’
- 修复方法:转到 Runner 目标的 Build Phases,并将
Embed Foundation Extension
移动到Thin Binary
的顶部。
- 修复方法:转到 Runner 目标的 Build Phases,并将
-
错误:App does not build after adding Share Extension?
- 修复方法:检查分享扩展的 Build Settings,移除所有从主项目导入 CocoaPods 的内容(即删除
Linking/Other Linker Flags
下的所有内容)。
- 修复方法:检查分享扩展的 Build Settings,移除所有从主项目导入 CocoaPods 的内容(即删除
-
可能需要为扩展目标禁用 bitcode
-
错误:Invalid Bundle. The bundle at ‘Runner.app/Plugins/Sharing Extension.appex’ contains disallowed file 'Frameworks’
- 修复方法:参考 StackOverflow
完整示例
以下是一个完整的示例代码,展示如何使用 receive_sharing_intent_ohos
插件来接收共享文件。
示例代码
main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:receive_sharing_intent_ohos/receive_sharing_intent_ohos.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late StreamSubscription _intentSub;
final _sharedFiles = <SharedMediaFile>[];
[@override](/user/override)
void initState() {
super.initState();
// 监听应用内存中接收到的媒体共享。
_intentSub = ReceiveSharingIntent.instance.getMediaStream().listen((value) {
setState(() {
_sharedFiles.clear();
_sharedFiles.addAll(value);
print(_sharedFiles.map((f) => f.toMap()));
});
}, onError: (err) {
print("getIntentDataStream error: $err");
});
// 获取应用关闭时接收到的媒体共享。
ReceiveSharingIntent.instance.getInitialMedia().then((value) {
setState(() {
_sharedFiles.clear();
_sharedFiles.addAll(value);
print(_sharedFiles.map((f) => f.toMap()));
// 告知库我们已完成处理意图。
ReceiveSharingIntent.instance.reset();
});
});
}
[@override](/user/override)
void dispose() {
_intentSub.cancel();
super.dispose();
}
[@override](/user/override)
Widget build(BuildContext context) {
const textStyleBold = const TextStyle(fontWeight: FontWeight.bold);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('插件示例应用'),
),
body: Center(
child: Column(
children: <Widget>[
Text("共享的文件:", style: textStyleBold),
Text(_sharedFiles
.map((f) => f.toMap())
.join(",\n****************\n")),
],
),
),
),
);
}
}
更多关于Flutter接收共享意图插件receive_sharing_intent_ohos的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter接收共享意图插件receive_sharing_intent_ohos的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
receive_sharing_intent_ohos
是一个用于在 Flutter 应用中接收共享意图(Sharing Intent)的插件,特别适用于 OpenHarmony(OHOS)平台。它允许你的应用接收来自其他应用共享的内容,例如文本、图片、文件等。
以下是使用 receive_sharing_intent_ohos
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 receive_sharing_intent_ohos
插件的依赖:
dependencies:
flutter:
sdk: flutter
receive_sharing_intent_ohos: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 初始化插件
在你的 Flutter 应用中初始化插件。通常,你可以在 main.dart
文件中进行初始化。
import 'package:flutter/material.dart';
import 'package:receive_sharing_intent_ohos/receive_sharing_intent_ohos.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Receive Sharing Intent Example',
home: SharingIntentScreen(),
);
}
}
3. 接收共享内容
在 SharingIntentScreen
中,你可以使用 ReceiveSharingIntentOhos
来接收共享的内容。
import 'package:flutter/material.dart';
import 'package:receive_sharing_intent_ohos/receive_sharing_intent_ohos.dart';
class SharingIntentScreen extends StatefulWidget {
[@override](/user/override)
_SharingIntentScreenState createState() => _SharingIntentScreenState();
}
class _SharingIntentScreenState extends State<SharingIntentScreen> {
String? _sharedText;
List<SharedMediaFile>? _sharedFiles;
[@override](/user/override)
void initState() {
super.initState();
// 接收共享的文本
ReceiveSharingIntentOhos.getTextStream().listen((String text) {
setState(() {
_sharedText = text;
});
}, onError: (err) {
print("Error receiving shared text: $err");
});
// 接收共享的文件
ReceiveSharingIntentOhos.getMediaStream().listen((List<SharedMediaFile> files) {
setState(() {
_sharedFiles = files;
});
}, onError: (err) {
print("Error receiving shared files: $err");
});
// 获取初始共享内容
ReceiveSharingIntentOhos.getInitialText().then((String? text) {
setState(() {
_sharedText = text;
});
});
ReceiveSharingIntentOhos.getInitialMedia().then((List<SharedMediaFile>? files) {
setState(() {
_sharedFiles = files;
});
});
}
[@override](/user/override)
void dispose() {
ReceiveSharingIntentOhos.reset();
super.dispose();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Receive Sharing Intent'),
),
body: Column(
children: [
if (_sharedText != null)
Text('Shared Text: $_sharedText'),
if (_sharedFiles != null)
Expanded(
child: ListView.builder(
itemCount: _sharedFiles!.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('File ${index + 1}'),
subtitle: Text(_sharedFiles![index].path),
);
},
),
),
],
),
);
}
}