首页 > 其他分享 >harmony_flutter 实现拨打电话功能

harmony_flutter 实现拨打电话功能

时间:2024-12-18 09:33:12浏览次数:6  
标签:拨打 number harmony ohos import null flutter MethodChannel

harmony_flutter 实现拨打电话功能

ohos端建立通

import call from '@ohos.telephony.call';
import {
  FlutterPlugin,
  FlutterPluginBinding
} from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/FlutterPlugin';
import { MethodCallHandler, MethodResult } from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel';
import MethodChannel from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel';
import MethodCall from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodCall';
import Log from '@ohos/flutter_ohos/src/main/ets/util/Log';
import deviceInfo from '@ohos.deviceInfo';

import bundleManager from '@ohos.bundle.bundleManager'

let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO

const TAG: string = "FlutterPhoneDirectCaller"

const CHANNEL_NAME = "flutter_phone_direct_caller";

export class FlutterPhoneDirectCallerPlugin implements FlutterPlugin, MethodCallHandler {
  getUniqueClassName(): string {
    return "FlutterPhoneDirectCallerPlugin";
  }

  private methodChannel: MethodChannel | null = null;
  private applicationContext: Context | null = null;

  onAttachedToEngine(binding: FlutterPluginBinding): void {
    this.applicationContext = binding.getApplicationContext();
    this.methodChannel = new MethodChannel(binding.getBinaryMessenger(), CHANNEL_NAME);
    this.methodChannel.setMethodCallHandler(this);
  }

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

  onMethodCall(call: MethodCall, result: MethodResult): void {
    try {
      if (call.method == "callNumber") {
        const number: string = call.argument("number")
        Log.i(TAG, deviceInfo.osFullName)
        this.callNumber(number).then((isSuccess) => {
          result.success(isSuccess);
        });
      } else {
        result.notImplemented()
      }
    } catch (err) {
      result.error("Name not found", err.message, null)
    }
  }

  async callNumber(number: string) {
    try {
      await call.makeCall(number)
      return true;
    } catch (e) {
      return false;
    }
  }
}

flutter端代码拨打电话类代码封装

import 'dart:async';

import 'package:flutter/services.dart';

class FlutterPhoneDirectCaller {
  static const MethodChannel _channel =
      MethodChannel('flutter_phone_direct_caller');

  static Future<bool?> callNumber(String number) async {
    return await _channel.invokeMethod(
      'callNumber',
      <String, Object>{
        'number': number,
      },
    );
  }
}

这样就能通过调用FlutterPhoneDirectCaller的callNumber方法来实现拨打电话的功能了.

标签:拨打,number,harmony,ohos,import,null,flutter,MethodChannel
From: https://www.cnblogs.com/hongmengos/p/18613900

相关文章

  • flutter_ohos_原生和flutter交互跳转
    flutter_ohos_原生和flutter交互跳转1.EntryAbility可以继承UIAbilityexportdefaultclassEntryAbilityextendsUIAbilityimplementsExclusiveAppComponent<UIAbility>{detachFromFlutterEngine():void{//thrownewError('Methodnotimplemented.�......
  • flutter_ohos_sequence
    性能分析第一步-梳理线程顺序Flutter应用在性能分析的时候,需要使用分析工具去抓取应用的trace,并对trace进行分析。Flutter应用在渲染流程上与几个线程密不可分,而这篇指南里,主要介绍这几个密不可分的线程,以及它们在渲染流程上顺序。分析工具常用的分析工具有DevEcoStudioProfi......
  • Flutter OHOS fluttertpc_screenshot_callback
    screenshot_callback监听系统截图注意事项Android需要获取存储权限才能正常使用需要真机才能验证截图Flutter中使用注意如果在flutter中重写didChangeAppLifecycleState,其中在三星手机上,侧面截图功能会执行resumed生命周期。caseAppLifecycleState.resumed://应用程......
  • Flutter OHOS fluttertpc_device_util
    fluttertpc_device_util鸿蒙设备工具类Flutter代码调用创建通道staticconstMethodChannel_channel=constMethodChannel('device_util');获取versionname///TheversionnameofthisapplicationstaticFuture<StringgetversionNameasync{finalString......
  • Flutter OHOS flutter_native_image
    flutter_native_image原生Flutter图片工具此插件旨在提供原生工具来调整图像大小并通过压缩降低其质量。代码有点粗糙(尤其是iOS部分),但它可以满足我的需求,并且从未崩溃过。如果您愿意,请随意改进它。用法安装在依赖项下的pubspec.yaml中添加以下几行flutter_native_imag......
  • Flutter OHOS flutter_gpu_image(图片视频添加滤镜)
    GPUImageforFlutterFlutter中相机、照片、视频添加各种滤镜效果。本地环境[✓]Flutter(Channelstable,3.0.0,onmacOS12.3.121E258darwin-x64,localezh-Hans-CN)[✓]AndroidtoolchaindevelopforAndroiddevices(AndroidSDKversion33.0.0-rc1)[✓]Xcod......
  • 鸿蒙Flutter怎样调试dart代码
    鸿蒙Flutter怎么样调试dart代码ets代码使用DevEco-Studio进行调试。dart代码可以使用vscode和AndroidStudio进行调试。flutter鸿蒙化版本调试时,需要加上参数,如:--local-engine=/Users/xxx/ohos/engine/src/out/ohos_debug_unopt_arm64使用vscode调试dart代......
  • 鸿蒙Flutter性能调优之性能分析定界
    鸿蒙Flutter性能调优之性能分析定界flutter鸿蒙化的工程,也可以使用devtools对Dart代码进行调试.前置条件OpenHarmonyNext系统前台运行Flutter页面分析工具DevEcoStudioProfilerSmartPerfFlutter线程介绍Flutter使用多个线程来完成其必要的工作,图层中仅展示了其中两......
  • 鸿蒙Flutter环境相关问题解决方法
    鸿蒙Flutter环境相关问题建议使用的开发工具版本flutter3.7.12-ohos版本python3.8-python3.11java17node18ohpm1.6+HamonyOSSDKapi11Xcode14.3断网环境flutterpubget执行失败解决方案:加上--offline参数,完整命令flutterpubget--offlinemac环境releas......
  • HarmonyOS NEXT基础入门
    基础入门储存鸿蒙应用提供多种数据存储方式,包括:Preferences:轻量级数据存储,适用于简单键值对。SQLite:关系型数据库,适用于结构化数据存储。文件存储:通过文件系统直接存储数据。数组数组是一组有序的数据集合,可以通过索引访问每个元素。letnumbers=[1,2,3,4,5];con......