YYPOST群发软件 发表于 2025-12-17 08:45:59

AIWROK软件device相关方法获取设备信息例子

AIWROK软件device相关方法获取设备信息例子
/**
* 设备信息获取示例
* //🍎交流QQ群711841924群一,苹果内测群,528816639
* 本示例展示了如何使用AIWork IDE提供的device相关方法获取设备信息
* 注意:某些方法可能在不同设备或系统版本上不可用,需要做好异常处理
*/

// 安全调用函数,防止方法不存在时报错
function safeCall(methodName, caller, args) {
    try {
      if (typeof caller === 'function') {
            if (args) {
                return caller(args);
            } else {
                return caller();
            }
      } else {
            return "方法不可用";
      }
    } catch (e) {
      return "调用出错: " + e.message;
    }
}

// 打印设备信息的函数
function printDeviceInfo() {
    printl("================== 设备信息 ==================");
   
    // 获取设备ID(字符串形式)
    var deviceID = safeCall("getDeviceID", device);
    printl("设备ID: " + deviceID);
   
    // 获取设备ID(整数形式)
    var deviceIntID = safeCall("getDeviceIntID", device);
    printl("设备整数ID: " + deviceIntID);
   
    // 获取IMEI
    var imei = safeCall("getIMEI", device);
    printl("IMEI: " + imei);
   
    // 获取IP地址
    var ip = safeCall("getIP", device);
    printl("IP地址: " + ip);
   
    // 获取OAID
    var oaid = safeCall("getOAID", device);
    printl("OAID: " + oaid);
   
    // 获取系统版本
    var version = safeCall("getVersion", device);
    printl("系统版本: " + version);
   
    // 获取设备型号
    var model = safeCall("getModel", device);
    printl("设备型号: " + model);
   
    // 获取设备品牌
    var brand = safeCall("getBrand", device);
    printl("设备品牌: " + brand);
   
    printl("=============================================");
}

// 显示设备屏幕信息
function printScreenInfo() {
    printl("================= 屏幕信息 =================");
   
    try {
      // 尝试多种方式获取屏幕分辨率
      var width, height;
      
      // 方式1: 尝试使用screen对象获取屏幕信息
      if (typeof screen !== 'undefined') {
            if (typeof screen.getScreenWidth === 'function') {
                width = screen.getScreenWidth();
            }
            if (typeof screen.getScreenHeight === 'function') {
                height = screen.getScreenHeight();
            }
      }
      
      // 方式2: 如果screen对象不可用,尝试其他方式
      if ((!width || !height) && typeof device !== 'undefined') {
            if (typeof device.getScreenWidth === 'function') {
                width = device.getScreenWidth();
            }
            if (typeof device.getScreenHeight === 'function') {
                height = device.getScreenHeight();
            }
      }
      
      if (width && height) {
            printl("屏幕分辨率: " + width + " x " + height);
      } else {
            printl("屏幕分辨率: 无法获取");
      }
    } catch (e) {
      printl("屏幕分辨率: 获取失败 (" + e.message + ")");
    }
   
    printl("=============================================");
}

// 显示电池信息
function printBatteryInfo() {
    printl("================= 电池信息 =================");
   
    try {
      var batteryLevel = "无法获取";
      
      // 尝试获取电池电量
      if (typeof device !== 'undefined') {
            if (typeof device.getBatteryLevel === 'function') {
                batteryLevel = device.getBatteryLevel();
                // 检查返回值是否有效
                if (batteryLevel === null || batteryLevel === undefined) {
                  batteryLevel = "无法获取";
                }
            }
      }
      
      printl("电池电量: " + batteryLevel + "%");
    } catch (e) {
      printl("电池信息: 获取失败 (" + e.message + ")");
    }
   
    printl("=============================================");
}

// 显示存储信息
function printStorageInfo() {
    printl("================= 存储信息 =================");
   
    try {
      // 尝试使用file对象获取存储信息
      if (typeof file !== 'undefined') {
            var hasValidMethod = false;
            
            // 获取SD卡可用空间
            if (typeof file.getFreeSpace === 'function') {
                try {
                  var freeSpace = file.getFreeSpace('/sdcard/');
                  if (freeSpace && freeSpace > 0) {
                        // 转换为MB显示
                        var freeSpaceMB = Math.round(freeSpace / (1024 * 1024));
                        printl("SD卡可用空间: " + freeSpaceMB + " MB");
                        hasValidMethod = true;
                  }
                } catch (e) {
                  // 忽略单个方法的错误
                }
            }
            
            // 获取SD卡总空间
            if (typeof file.getTotalSpace === 'function') {
                try {
                  var totalSpace = file.getTotalSpace('/sdcard/');
                  if (totalSpace && totalSpace > 0) {
                        // 转换为MB显示
                        var totalSpaceMB = Math.round(totalSpace / (1024 * 1024));
                        printl("SD卡总空间: " + totalSpaceMB + " MB");
                        hasValidMethod = true;
                  }
                } catch (e) {
                  // 忽略单个方法的错误
                }
            }
            
            if (!hasValidMethod) {
                printl("存储信息: 无法获取有效的存储信息");
            }
      } else {
            printl("存储信息: file对象不可用");
      }
    } catch (e) {
      printl("存储信息: 获取失败 (" + e.message + ")");
    }
   
    printl("=============================================");
}

// 显示内存信息
function printMemoryInfo() {
    printl("================= 内存信息 =================");
   
    try {
      var memoryInfo = "无法获取";
      var memoryPercent = "无法获取";
      
      // 尝试获取内存信息
      if (typeof app !== 'undefined') {
            if (typeof app.getMemory === 'function') {
                memoryInfo = app.getMemory();
            }
            
            if (typeof app.getMemoryPercent === 'function') {
                memoryPercent = app.getMemoryPercent();
            }
      }
      
      // 解析内存信息(如果是JSON格式)
      if (typeof memoryInfo === "string" && memoryInfo.indexOf("{") === 0) {
            try {
                var memObj = JSON.parse(memoryInfo);
                printl("总内存: " + memObj.Total + " MB");
                printl("可用内存: " + memObj.Available + " MB");
                printl("已用内存: " + memObj.Used + " MB");
                printl("应用内存: " + memObj.app + " MB");
            } catch (parseError) {
                printl("内存信息: " + memoryInfo);
            }
      } else {
            printl("内存信息: " + memoryInfo);
      }
      
      printl("内存使用率: " + memoryPercent + "%");
    } catch (e) {
      printl("内存信息: 获取失败 (" + e.message + ")");
    }
   
    printl("=============================================");
}

// 显示当前运行应用信息
function printRunningAppInfo() {
    printl("================= 应用信息 =================");
   
    try {
      // 获取当前顶端应用包名
      if (typeof app !== 'undefined') {
            if (typeof app.getTopPackName === 'function') {
                var topPackageName = app.getTopPackName();
                if (topPackageName) {
                  printl("当前应用包名: " + topPackageName);
                  
                  // 尝试获取应用名称
                  if (typeof app.getAppName === 'function') {
                        var appName = app.getAppName(topPackageName);
                        if (appName) {
                            printl("当前应用名称: " + appName);
                        }
                  }
                } else {
                  printl("当前应用包名: 无法获取");
                }
            } else {
                printl("获取应用信息: getTopPackName方法不可用");
            }
      } else {
            printl("获取应用信息: app对象不可用");
      }
    } catch (e) {
      printl("获取应用信息失败: " + e.message);
    }
   
    printl("=============================================");
}

// 显示网络信息
function printNetworkInfo() {
    printl("================= 网络信息 =================");
   
    try {
      var hasNetworkInfo = false;
      
      // 获取WiFi信息
      if (typeof wifi !== 'undefined') {
            if (typeof wifi.isWifiEnabled === 'function') {
                var isWifiEnabled = wifi.isWifiEnabled();
                printl("WiFi状态: " + (isWifiEnabled ? "已启用" : "已禁用"));
                hasNetworkInfo = true;
            }
            
            // 获取WiFi名称
            if (typeof wifi.getWifiName === 'function') {
                var wifiName = wifi.getWifiName();
                if (wifiName) {
                  printl("WiFi名称: " + wifiName);
                  hasNetworkInfo = true;
                }
            }
      }
      
      
      // 如果没有任何网络信息可用
      if (!hasNetworkInfo) {
            printl("网络信息: 无法获取有效的网络信息");
      }
    } catch (e) {
      printl("网络信息: 获取失败 (" + e.message + ")");
    }
   
    printl("=============================================");
}

// 主函数
function main() {
    printl("设备信息获取示例开始执行...");
   
    // 打印设备基本信息
    printDeviceInfo();
   
    // 打印屏幕信息
    printScreenInfo();
   
    // 打印电池信息
    printBatteryInfo();
   
    // 打印存储信息
    printStorageInfo();
   
    // 打印内存信息
    printMemoryInfo();
   
    // 打印当前运行应用信息
    printRunningAppInfo();
   
    // 打印网络信息
    printNetworkInfo();
   
    printl("设备信息获取示例执行完毕。");
}

// 执行主函数
main();

页: [1]
查看完整版本: AIWROK软件device相关方法获取设备信息例子