flutter 命令行方式生成签名证书
需要对现有 app 进行打包,然后查看当前包的证书信息
flutter build apk
keytool -printcert -jarfile app-debug.apk
查看证书示例
生成新的签名证书
keytool -genkey -v -keystore test.keystore -alias demo01 -keyalg RSA -validity 4000
flutter 3.0 查看安卓项目包名
路径 project\android\app\build.gradle
...
android {
namespace "com.example.demo01"
...
}
...
高德开放平台申请Key
根据上面的查询结果填写到对应的表单提交后得到 Key
配置 flutter 安卓文件
配置 project\android\app\src\main\AndroidManifest.xml
添加权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+++++
<!--访问网络-->
<uses-permission android:name="android.permission.INTERNET" />
<!--粗略定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--精确定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--申请调用 A-GPS 模块-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<!--用于获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--用于访问 wifi 网络信息,wifi 信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--用于获取 wifi 的获取权限,wifi 信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!--用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--用于写入缓存数据到扩展存储卡-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+++++
....
</manifest>
配置 project\android\app\build.gradle
文件
android {
...
+++
signingConfigs {
release {//发布版本的签名配置
storeFile file('test.keystore')
keyAlias "demo01" // 和证书的别名对应
storePassword "gongjian" // 和签名私钥一致
keyPassword "gongjian" // 和签名私钥一致
}
debug {//调试版本的签名配置
storeFile file('test.keystore')
keyAlias "demo01" // 和证书的别名对应
storePassword "gongjian" // 和签名私钥一致
keyPassword "gongjian" // 和签名私钥一致
}
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
debug {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
+++
}
dependencies {
implementation('com.amap.api:location:5.2.0')
}
注意需要将证书复制到 project\android\app
目录下
安装高德地图官方依赖调用定位API
amap_flutter_location: ^3.0.0
permission_handler: ^11.3.1
在 main.dart
中使用
import 'dart:convert';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';
import 'package:permission_handler/permission_handler.dart';
class PageComponent extends StatefulWidget {
const PageComponent({super.key});
@override
State<PageComponent> createState() => _PageComponentState();
}
class _PageComponentState extends State<PageComponent> {
/**获取数据 */
Map<String, Object>? _locationResult;
/**监听定位 */
StreamSubscription<Map<String, Object>>? _locationListener;
/**实例化插件 */
AMapFlutterLocation _locationPlugin = new AMapFlutterLocation();
@override
void initState() {
super.initState();
/// 动态申请定位权限
requestPermission();
/// [hasShow] 隐私权政策是否弹窗展示告知用户
AMapFlutterLocation.updatePrivacyShow(true, true);
/// [hasAgree] 隐私权政策是否已经取得用户同意
AMapFlutterLocation.updatePrivacyAgree(true);
AMapFlutterLocation.setApiKey("安卓的key", "ios的key");
///注册定位结果监听
_locationListener = _locationPlugin.onLocationChanged().listen((Map<String, Object> result) {
setState(() {
_locationResult = result;
});
print("定位开启成功 ${json.encode(result)}");
});
}
/// 动态申请定位权限
void requestPermission() async {
// 申请权限
bool hasLocationPermission = await requestLocationPermission();
if (hasLocationPermission) {
print("定位权限申请通过");
} else {
print("定位权限申请不通过");
}
}
// 申请定位权限
/// 授予定位权限返回true, 否则返回false
Future<bool> requestLocationPermission() async {
//获取当前的权限
var status = await Permission.location.status;
if (status == PermissionStatus.granted) {
//已经授权
return true;
} else {
//未授权则发起一次申请
status = await Permission.location.request();
if (status == PermissionStatus.granted) {
return true;
} else {
return false;
}
}
}
// 开启定位
void _startLocation() {
///开始定位之前设置定位参数
// _setLocationOption();
_locationPlugin.startLocation();
}
// 停止定位
void _stopLocation() {
_locationPlugin.stopLocation();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("PageComponent页面")),
body:Container(
child: Column(
children: [
ElevatedButton(onPressed: (){_startLocation();}, child: Text("开启定位")),
ElevatedButton(onPressed: (){_stopLocation();}, child: Text("关闭定位")),
Text("定位结果:${_locationResult.toString()}"),
],
),
)
);
}
}