首页 > 其他分享 >harmony_flutter_udid

harmony_flutter_udid

时间:2024-12-18 09:23:41浏览次数:4  
标签:applicationContext return udid harmony null flutter channel

harmony_flutter 获取udid

UDID说明:在恢复出厂设置后,UDID(唯一设备标识符)可能会发生变化!另外,如果设备通过OTA(在线更新)升级到了Android 8.0,并且应用程序被重新安装了,由于Android 8.0的安全性更改,UDID也可能会改变。对于已经获取root权限或越狱的设备,其ID是可以被更改的,请注意这一点。不过,由于ID的复杂性,通过随机猜测来冒充另一个已存在的用户应该是不可能实现的。

ohos端建立通道获取系统udid

import identifier from '@ohos.identifier.oaid';//获取odid库

export default class FlutterUdidPlugin implements MethodCallHandler, FlutterPlugin {
  private channel: MethodChannel | null = null
  private applicationContext: common.Context | null = null
  private ability: UIAbility | null = null;

  constructor(context?: common.Context) {
    if (context) {
      this.applicationContext = context;
    }
  }

  static registerWith(): void {
  }

  getUniqueClassName(): string {
    return TAG;
  }

  onAttachedToEngine(binding: FlutterPluginBinding): void {
    this.onAttachedToEngine1(binding.getApplicationContext(), binding.getBinaryMessenger());
  }

  private onAttachedToEngine1(applicationContext: Context, messenger: BinaryMessenger) {
    this.applicationContext = applicationContext;
    this.channel = new MethodChannel(messenger, "flutter_udid")
    this.channel.setMethodCallHandler(this)
  }

  onAttachedToAbility(binding: AbilityPluginBinding): void {
    this.ability = binding.getAbility()
  }

  onDetachedFromAbility(): void {
    this.ability = null;
  }

  onMethodCall(call: MethodCall, result: MethodResult): void {
    if (call.method == "getUDID") {
      this.requestPermissions().then((data: boolean) => {
        this.getUDID().then((udid: string | null) => {
          if (udid == null || udid == "") {
            result.error("UNAVAILABLE", "UDID not available.", null)
          } else {
          	//传递到flutter层
            result.success(udid)
          }
        })
      })
    } else {
      result.notImplemented()
    }
  }

  onDetachedFromEngine(binding: FlutterPluginBinding): void {
    this.applicationContext = null;
    this.channel?.setMethodCallHandler(null)
  }

  //ohos端获取oaid
  private async getUDID(): Promise<string | null> {
    try {
      return await identifier.getOAID()
    } catch (err) {
      return null
    }
  }

  private async requestPermissions(): Promise<boolean> {
    if (!this.ability) {
      Log.i(TAG, "Could not launch BarcodeScanner because the plugin is not attached to any ability")
      return false
    }
    try {
      const results = await requestPermissions(this.ability!.context)
      return results ? results : false
    } catch (e) {
    }
    return false
  }
}

flutter端代码


class FlutterUdid {
  //建立通道
  static const MethodChannel _channel = const MethodChannel('flutter_udid');

  /// Returns the UDID in the platform-specific format.
  /// iOS: 7946DA4E-8429-423C-B405-B3FC77914E3E,
  /// Android: 8af8770a27cfd182
  static Future<String> get udid async {
    final String udid = await _channel.invokeMethod('getUDID');
    return udid;
  }

  /// Returns the UDID in a consistent format for all platforms.
  /// Example: 984725b6c4f55963cc52fca0f943f9a8060b1c71900d542c79669b6dc718a64b
  static Future<String> get consistentUdid async {
    final String udid = await _channel.invokeMethod('getUDID');
    var bytes = utf8.encode(udid);
    var digest = sha256.convert(bytes);
    return digest.toString();
  }
}

flutter example 引用

import 'package:flutter_udid/flutter_udid.dart';

String udid = await FlutterUdid.udid;

标签:applicationContext,return,udid,harmony,null,flutter,channel
From: https://www.cnblogs.com/hongmengos/p/18613867

相关文章

  • harmony_flutter_FlutterEngineGroup
    harmony_flutter_FlutterEngineGroup介绍1.EntryAbility修改为继承自UIAbilityexportdefaultclassEntryAbilityextendsUIAbilityimplementsExclusiveAppComponent<UIAbility>{detachFromFlutterEngine():void{//thrownewError('Methodnotimplem......
  • flutter_ohos_5
    ohos开发鸿蒙(基于5.0.0版本)原始仓来源:https://github.com/flutter/flutter1.仓库说明本仓库是基于FlutterSDK对于OpenHarmony平台的兼容拓展,可支持IDE或者终端使用FlutterTools指令编译和构建OpenHarmony应用程序。2.环境依赖说明:1.FlutterTools指令目前已支持在Linux......
  • Flutter OHOS harmony_fluwx 集成微信服务(二)
    harmony_fluwx集成微信服务(2)fluwx链接:https://gitee.com/almost777/fluwx接入功能分享图片,文本,音乐,视频等。支持分享到会话,朋友圈以及收藏.微信支付.在微信登录时,获取AuthCode.拉起小程序.订阅消息.打开微信.从微信标签打开应用初始化注册WxAPIregisterWxApi(ap......
  • Flutter OHOS flutter_dart_native
    DartNativeDartNative作为Dart和原生API之间沟通的桥梁。用更快、更简洁的代码替换性能低下的Flutter通道。特征动态同步和异步通道DartNative动态调用任何原生API。它支持同步和异步通道。多语言接口直接调用不再需要像FlutterChannel那样对参数和返回值进行......
  • Flutter OHOS flutter appscheme插件
    FlutterAppScheme配置说明1、Android端配置说明在您项目中Android的AndroidManifest.xml文件中按照如下规范添加Scheme,例如android/app/src/main/AndroidManifest.xmla、在需要启动的Activity中新增以下格式的代码<!--AndroidScheme--><intent-filter><actionandro......
  • 四大跨平台开发框架深度解析——uniapp、uniapp-X、React Native与Flutter
    引言随着移动互联网的飞速发展,跨平台开发框架成为了开发者们关注的焦点。这些框架旨在通过编写一套代码,实现多个平台的应用开发,从而大幅提高开发效率和降低维护成本。本文将深入剖析uniapp、uniapp-X、ReactNative和Flutter这四个主流的跨平台开发框架,探讨它们的优缺点及......
  • 「九」HarmonyOS 5 端云一体化实战项目——「M.U.」应用云侧开发云数据库
    1立意背景M.代表“我”,U.代表“你”,这是一款用于记录情侣从相识、相知、相恋、见家长、订婚直至结婚等各个阶段美好记忆留存的应用程序。它旨在为情侣们提供一个专属的空间,让他们能够将一路走来的点点滴滴,如初次相遇时的心动瞬间、逐渐了解彼此过程中的深入交流、甜蜜......
  • HarmonyOS应用开发--LazyForeach懒加载
    引言在鸿蒙应用开发中,ArkTS(ArkTypeScript)作为开发语言,提供了丰富的API来构建高效、流畅的应用程序。其中,LazyForeach作为一种懒加载技术,对于优化性能和提升用户体验具有重要作用。本文将对LazyForeach懒加载进行深入学习总结。ArkTS简介ArkTS是基于TypeScript的开发语言,专......
  • OpenHarmony测试RS232/RS485串口方法,触觉智能SBC3528工控主板演示
    教大家介绍在OpenHarmony系统,没有串口工具的情况下如何测试RS232/RS485,使用触觉智能SBC3528工控主板演示,搭载了瑞芯微RK3568四核处理器,板载2路RS232+4路隔离RS485,集成DIDO,自研RS485自动收发驱动,支持超2KM传输距离!RS485测试方法以触觉智能SBC3528工控主板为例,如果需要测试RS485串......
  • HarmonyOS 获取进程相关的信息process 常用的几个方法
    获取进程相关的信息,提供进程管理的相关功能。process1.EventListener2.isIsolatedProcess3.is64Bit4.getStartRealtime5.getPastCpuTime导入模块import{process}from'@kit.ArkTS';属性名称类型可读可写说明uidnumber是否进程的用户标识。pidnumber是......