flutter中如何获取app信息及设备信息
app开发中不可避免要动态获取当前版本号、包名以及设备信息等,安卓原生可以使用如下方法
1 获取app VersionName:
public static String getAppVersionName(final String packageName) {
if (isSpace(packageName)) return "";
try {
PackageManager pm = Utils.getApp().getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
return pi == null ? null : pi.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "";
}
}
2 获取app VersionCode:
public static int getAppVersionCode(final String packageName) {
if (isSpace(packageName)) return -1;
try {
PackageManager pm = Utils.getApp().getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
return pi == null ? -1 : pi.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return -1;
}
}
3 获取包名:
public static String getAppName(final String packageName) {
if (isSpace(packageName)) return "";
try {
PackageManager pm = Utils.getApp().getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
return pi == null ? null : pi.applicationInfo.loadLabel(pm).toString();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "";
}
}
以及获取设备信息:
public static String getDevicesType() {
model = android.os.Build.MODEL;//手机型号
carrier = Build.MANUFACTURER;//手机厂商
LogUtils.d("model== " + model + " carrie=r=" + carrier);
return carrier;
}
那么在flutter项目中应该日和获取这些信息呢?
ok 直接上代码
首先添加两个依赖
package_info 和 device_info_plus(之前的device_info已经弃用)
在项目pubspec.yaml文件中添加
package_info: ^2.0.2
device_info_plus: ^8.2.2
在需要使用的分别导包
import ‘package:device_info_plus/device_info_plus.dart’;
import ‘package:package_info/package_info.dart’;
获取app信息:
static Future<String> getAppInfo() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
_appName = packageInfo.appName;
_packageName = packageInfo.packageName;
_version = packageInfo.version;
_buildNumber = packageInfo.buildNumber;
LogD("_appName=" +
_appName +
" _packageName=" +
_packageName +
" _version=" +
_version);
return _appName + _packageName + _version + _buildNumber;
}
获取设备信息:
//设备机型判断
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
LogD('devices model : ${androidInfo.model}'); // e.g. "Moto G (4)"
if (androidInfo.model == "手持端") {
//todo do something
}
如果是ios设备则文章来源:https://www.toymoban.com/news/detail-624627.html
IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
运行结果如图:
文章来源地址https://www.toymoban.com/news/detail-624627.html
到了这里,关于flutter中如何获取app信息及设备信息,看这就够了的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!