我们在本章回中获取的设备信息主要指手机的硬件和软件参数,比如手机屏幕的分辨率,手机上系统的版本号。如果是原生开发的话,使用官方提供的接口就可以实现,但
是在Flutter开发中没有类似的接口,怎么办呢?本章回中将给大家介绍如何去获取这些设备信息。
我们想要获取的设备信息可以通过device_info_plus这个三方包实现,该包提供了相关的接口来获取设备信息。详细的使用方法如下:
设备信息以类的成员属性存放在类的对象中,下面是Android设备的成员属性,请大家参考:
/// Android operating system version values derived from `android.os.Build.VERSION`.
final AndroidBuildVersion version;
/// The name of the underlying board, like "goldfish".
/// https://developer.android.com/reference/android/os/Build#BOARD
final String board;
/// The system bootloader version number.
/// https://developer.android.com/reference/android/os/Build#BOOTLOADER
final String bootloader;
/// The consumer-visible brand with which the product/hardware will be associated, if any.
/// https://developer.android.com/reference/android/os/Build#BRAND
final String brand;
/// The name of the industrial design.
/// https://developer.android.com/reference/android/os/Build#DEVICE
final String device;
/// A build ID string meant for displaying to the user.
/// https://developer.android.com/reference/android/os/Build#DISPLAY
final String display;
/// A string that uniquely identifies this build.
/// https://developer.android.com/reference/android/os/Build#FINGERPRINT
final String fingerprint;
/// The name of the hardware (from the kernel command line or /proc).
/// https://developer.android.com/reference/android/os/Build#HARDWARE
final String hardware;
/// Hostname.
/// https://developer.android.com/reference/android/os/Build#HOST
final String host;
/// Either a changelist number, or a label like "M4-rc20".
/// https://developer.android.com/reference/android/os/Build#ID
final String id;
/// The manufacturer of the product/hardware.
/// https://developer.android.com/reference/android/os/Build#MANUFACTURER
final String manufacturer;
/// `false` if the application is running in an emulator, `true` otherwise.
final bool isPhysicalDevice;
/// Information about the current android display.
final AndroidDisplayMetrics displayMetrics;
/// Hardware serial number of the device, if available
///
/// There are special restrictions on this identifier, more info here:
/// https://developer.android.com/reference/android/os/Build#getSerial()
final String serialNumber;
上面的代码来自包中的源代码,代码中的注释就是成员属性的含义,从中可以看到,我们可以获取到Android设备的硬件版本号,Android系统版本号等信息。此外,这
里只列出了部分信息,大家可以从源代码中看到完成的设备信息。关于该包更多的用法可以参考API文档。
///获取手机上的软件和硬件信息
Future<String> getAndroidDeviceInfo() async {
String result = "";
DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
AndroidDeviceInfo androidDeviceInfo = await deviceInfoPlugin.androidInfo;
result = androidDeviceInfo.toString();
debugPrint("device info: $result");
///可以得到以下关键信息
/// widthPx: 1080.0, heightPx: 2460 sdkInt: 33
return result;
}
上面是我们获取Android设备信息的示例代码,通过该代码可以获取当前手机的屏幕分辨率和手机上Android系统的版本号。大家可以自动动手来获取IOS手机上的设备
信息,就当作是我留给大家的作业吧。
最后,我们对本章回的内容做一个全面的总结: