如何在ionic4中获取来电号码?

问题描述

我使用了此插件:https://www.npmjs.com/package/cozzbie.plugin.phonecalltrap

使用此插件我只有“电话状态” (响铃,空闲,离线)。我要来电移动电话。 我还添加了READ_PHONE_STATE和READ_CONTACTS的Android权限,但我仍然无法获取手机号码。我不知道是否需要在AndroidManifest.xml文件中添加任何内容。

我在这里分享我的代码。

import { Component } from '@angular/core';
import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';

declare var PhoneCallTrap: any;
declare var window: any;

@Component({
  selector: 'app-root',templateUrl: 'app.component.html',styleUrls: ['app.component.scss']
})

export class AppComponent {
  constructor(
    private platform: Platform,private androidPermissions: AndroidPermissions,private splashScreen: SplashScreen,private statusBar: StatusBar
  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.checkPermissionCall();
      this.checkPermissionREAD_CONTACTS();
    });
  }
  
   checkPermissionCall() {
    this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_PHONE_STATE).then(
      success => {
        //if permission granted       
        console.log(success);      
      },err => {
        this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_PHONE_STATE).
          then(success => {          
            console.log(success);         
          },err => {
              console.log("cancelled")
            });
      });
    this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.READ_PHONE_STATE]);    
  }

  checkPermissionREAD_CONTACTS() {
    this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_CONTACTS).then(
    success => {        
      console.log(success);
    setTimeout(() => {
      this.phonecalls();
    },5000)
    //if permission granted
    },err => {
    this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_CONTACTS).
    then(success => {   
    console.log(success);
    setTimeout(() => {
    this.phonecalls();
    },5000)
     },err => {  
    });
    });
    this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.READ_CONTACTS]);    
    }
  
  phonecalls() {
    if (window.PhoneCallTrap) {
      window.PhoneCallTrap.onCall((obj) => {
      var callObj = JSON.parse(obj),state = callObj.state,callingNumber = callObj.incomingNumber;
        console.log("obj: " + obj);
        //working
        console.log("callingNumber STATE: " + callingNumber);
        switch (state) {
          case "RINGING":
            console.log("Phone is ringing");
            break;
          case "OFFHOOK":
            console.log("Phone is off-hook");
            break;
          case "IDLE":
            console.log("Phone is idle");
            break;
        }
      });
  }
}
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...