Flutter原生方法调用插件quake_android_native_methods的使用
夸克安卓原生方法插件
夸克安卓原生方法插件提供了丰富的功能,用于在Flutter应用中调用Android原生方法。以下是该插件的使用方法和详细示例。
使用方法:
调用Android原生方法
1. 从相册选择图片:
static Future<String?> chooseImgFromGallery() async {
final String? res = await _channel.invokeMethod('chooseImgFromGallery');
return res;
}
返回字段:
[
{
"picName":"IMG1929167290848708488.png",
"url":"/storage/emulated/0/FaceDetectionSystem/IMG1929167290848708488.png",
"w":1080,
"h":1920,
"imageUri":"content://media/external/images/media/8915"
},
{
"picName":"Screenshot_2021-10-02-16-30-35-339_com.lbe.security.miui.jpg",
"url":"/storage/emulated/0/DCIM/Screenshots/Screenshot_2021-10-02-16-30-35-339_com.lbe.security.miui.jpg",
"w":1080,
"h":1920,
"imageUri":"content://media/external/images/media/7723"
},
{
"picName":"takePhoto1632994658702.jpg",
"url":"/storage/emulated/0/Quake/Photograph/takePhoto1632994658702.jpg",
"w":3120,
"h":4160,
"imageUri":"content://media/external/images/media/7715"
}
]
表格说明:
返回字段 | 类型 | 说明 |
---|---|---|
picName | String | 图片名称 |
url | String | 图片本地路径 |
w | String | 图片宽 |
h | String | 图片高 |
imageUri | String | 图片uri |
2. 拍照:
static Future<String?> photograph() async {
final String? res = await _channel.invokeMethod('photograph');
return res;
}
返回字段:
{
"photoPath":"/storage/emulated/0/Quake/Photograph/takePhoto1635478818664.jpg",
"photoWidth":3120,
"photoHeight":4160,
"photoName":"takePhoto1635478818664.jpg"
}
表格说明:
返回字段 | 类型 | 说明 |
---|---|---|
photoPath | String | 照片本地路径 |
photoWidth | String | 照片宽 |
photoHeight | String | 照片高 |
photoName | String | 照片名称 |
3. 从视频选择器选择视频:
static Future<String?> chooseVideoFromSelector() async {
final String? res = await _channel.invokeMethod('chooseVideoFromSelector');
return res;
}
返回字段:
{
"videoName":"VID_20211027_163349.mp4",
"videoUrl":"/storage/emulated/0/DCIM/Camera/VID_20211027_163349.mp4",
"videoDuration":0,
"coverName":"videoCover1635478990411.jpeg",
"coverUrl":"/storage/emulated/0/Android/data/quake.quake_android_native_methods/cache/videoCover1635478990411.jpeg",
"coverWidth":2160,
"coverHeight":3840
}
表格说明:
返回字段 | 类型 | 说明 |
---|---|---|
videoName | String | 视频名称 |
videoUrl | String | 视频本地路径 |
videoDuration | String | 视频时长 |
coverName | String | 封面名称 |
coverUrl | String | 封面本地路径 |
coverWidth | String | 封面宽 |
coverHeight | String | 封面高 |
5. 录像:
static Future<String?> videoRecording() async {
final String? res = await _channel.invokeMethod('videoRecording');
return res;
}
返回字段:
{
"videoName":"VID_20211029_114600.mp4",
"videoUrl":"/storage/emulated/0/DCIM/Camera/VID_20211029_114600.mp4",
"videoDuration":1,
"coverName":"videoCover1635479165517.jpeg",
"coverUrl":"/storage/emulated/0/Android/data/quake.quake_android_native_methods/cache/videoCover1635479165517.jpeg",
"coverWidth":2160,
"coverHeight":3840
}
表格说明:
返回字段 | 类型 | 说明 |
---|---|---|
videoName | String | 视频名称 |
videoUrl | String | 视频本地路径 |
videoDuration | String | 视频时长 |
coverName | String | 封面名称 |
coverUrl | String | 封面本地路径 |
coverWidth | String | 封面宽 |
coverHeight | String | 封面高 |
6. 从文件选择器选择文件:
static Future<String?> chooseFileFromSelector() async {
final String? res = await _channel.invokeMethod('chooseFileFromSelector');
return res;
}
返回字段:
{
"fileName":"新建文本文档.txt",
"fileSize":47,
"fileUrl":"/storage/emulated/0/bluetooth/新建文本文档.txt"
}
表格说明:
返回字段 | 类型 | 说明 |
---|---|---|
fileName | String | 文件名称 |
fileSize | String | 文件大小 |
fileUrl | String | 文件本地路径 |
7. 浏览文件:
static Future<String?> browseFile(String url) async {
final String? res = await _channel
.invokeMethod('browseFile', <String, dynamic>{'url': url});
return res;
}
参数:
参数 | 类型 | 说明 |
---|---|---|
url | String | 文件路径 |
返回字段:
{
"snapShotUrl":"/storage/emulated/0/Quake/fileSnapshot/test/document/.jpg"
}
表格说明:
返回字段 | 类型 | 说明 |
---|---|---|
snapShotUrl | String | 截图存储路径 |
8. 发送定位:
static Future<String?> sendLocation() async {
final String? res = await _channel.invokeMethod('sendLocation');
return res;
}
返回字段:
{
"locScreenShotUrl":"/storage/emulated/0/Android/data/quake.quake_android_native_methods/cache/location_20211029115351.png",
"locScreenShotName":"location_20211029115351.png",
"location":"福建省福州市台江区新港街道五一中路大利嘉城(状元街11号)",
"latitude":26.067215,
"longitude":119.312812
}
表格说明:
返回字段 | 类型 | 说明 |
---|---|---|
locScreenShotUrl | String | 截图存储路径 |
locScreenShotName | String | 截图名称 |
location | String | 地址 |
latitude | Double | 纬度 |
longitude | Double | 经度 |
9. 扫描二维码:
static Future<String?> scanQRCode() async {
final String? res = await _channel.invokeMethod('scanQRCode');
return res;
}
返回字段:
# 成功
{
"result":"eyJvcmdJRCI6ICIxMyJ9",
"code":1500
}
# 失败
{
"result":"解析二维码失败",
"code":1400
}
表格说明:
返回字段 | 类型 | 说明 |
---|---|---|
result | String | 二维码解析结果 |
code | int | 1500:成功 \ 1400:失败 |
完整示例Demo
以下是一个完整的Flutter示例代码,展示了如何使用quake_android_native_methods
插件调用上述功能。
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:quake_android_native_methods/quake_android_native_methods.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// 初始化平台状态
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion = await QuakeAndroidNativeMethods.platformVersion ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: <Widget>[
Text('Running on: $_platformVersion\n'),
RaisedButton(
child: const Text(
'打开相册',
style: TextStyle(fontSize: 28.0),
),
onPressed: () async {
String? message = await QuakeAndroidNativeMethods.chooseImgFromGallery();
print("Dart层:" + message!);
},
),
RaisedButton(
child: const Text(
'拍照',
style: TextStyle(fontSize: 28.0),
),
onPressed: () async {
String? message = await QuakeAndroidNativeMethods.photograph();
print("Dart层:" + message!);
},
),
RaisedButton(
child: const Text(
'打开视频',
style: TextStyle(fontSize: 28.0),
),
onPressed: () async {
String? message = await QuakeAndroidNativeMethods.chooseVideoFromSelector();
print("Dart层:" + message!);
},
),
RaisedButton(
child: const Text(
'录像',
style: TextStyle(fontSize: 28.0),
),
onPressed: () async {
String? message = await QuakeAndroidNativeMethods.videoRecording();
print("Dart层:" + message!);
},
),
RaisedButton(
child: const Text(
'打开文件',
style: TextStyle(fontSize: 28.0),
),
onPressed: () async {
String? message = await QuakeAndroidNativeMethods.chooseFileFromSelector();
print("Dart层:" + message!);
},
),
RaisedButton(
child: const Text(
'查看文件',
style: TextStyle(fontSize: 28.0),
),
onPressed: () async {
String? message = await QuakeAndroidNativeMethods.browseFile('/storage/emulated/0/Quake/fileSnapshot/test/document/.jpg');
print("Dart层:" + message!);
},
),
RaisedButton(
child: const Text(
'发送定位',
style: TextStyle(fontSize: 28.0),
),
onPressed: () async {
String? message = await QuakeAndroidNativeMethods.sendLocation();
print("Dart层:" + message!);
},
),
RaisedButton(
child: const Text(
'扫描二维码',
style: TextStyle(fontSize: 28.0),
),
onPressed: () async {
String? message = await QuakeAndroidNativeMethods.scanQRCode();
print("Dart层:" + message!);
},
),
],
),
),
),
);
}
}
更多关于Flutter原生方法调用插件quake_android_native_methods的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter原生方法调用插件quake_android_native_methods的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在 Flutter 中,如果你需要调用 Android 原生代码,可以使用 MethodChannel
来实现。quake_android_native_methods
听起来像是一个自定义的插件,用于调用 Android 原生方法。以下是如何使用 MethodChannel
来调用 Android 原生方法的步骤。
1. 创建 Flutter 项目
首先,确保你已经创建了一个 Flutter 项目。
2. 在 Flutter 中设置 MethodChannel
在 Flutter 代码中,你可以使用 MethodChannel
来调用 Android 原生方法。
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Native Method Example'),
),
body: Center(
child: ElevatedButton(
onPressed: _callNativeMethod,
child: Text('Call Native Method'),
),
),
),
);
}
static const platform = MethodChannel('com.example.quake_android_native_methods/channel');
Future<void> _callNativeMethod() async {
try {
final String result = await platform.invokeMethod('quakeMethod');
print('Native method result: $result');
} on PlatformException catch (e) {
print("Failed to invoke native method: '${e.message}'.");
}
}
}
3. 在 Android 原生代码中实现 MethodChannel
在 Android 项目中,你需要实现 MethodChannel
并处理来自 Flutter 的调用。
-
打开
android/app/src/main/java/com/example/yourapp/MainActivity.java
(假设你的 Flutter 项目包名为com.example.yourapp
)。 -
在
MainActivity
中设置MethodChannel
:
package com.example.yourapp;
import android.os.Bundle;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.app.FlutterActivity;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.example.quake_android_native_methods/channel";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
(call, result) -> {
if (call.method.equals("quakeMethod")) {
String response = nativeQuakeMethod();
result.success(response);
} else {
result.notImplemented();
}
}
);
}
private String nativeQuakeMethod() {
// 这里是你的原生方法实现
return "Hello from Android Native!";
}
}