快速查看来电的电话号码 [英] Access phone number of incoming calls in flutter

查看:62
本文介绍了快速查看来电的电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何才能像扑朔迷离的呼叫者一样快速地获得来电的电话号码.

How can we access the phone number of an incoming call in flutter, like the one Truecaller does.

我在网上引用了该查询,发现flutter当前不支持该功能.是真的还是有某种方法可以访问来电的电话号码?

I referenced this query over the web and found that flutter currently doesn't supports that feature. Is it true or there is some way to access the phone number of an incoming call?

推荐答案

您必须通过平台频道:

> Android

添加到文件 android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<receiver android:name=".ServiceReceiver" >
<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>

创建文件 android/app/src/main/java/{your_package}/ServiceReceiver.java

public class ServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(new PhoneStateListener(){
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            System.out.println("incomingNumber : "+incomingNumber);
        }
    },PhoneStateListener.LISTEN_CALL_STATE);
}

> iPhone

要提供有关传入呼叫者的标识信息,请在beginRequest(with :)的实现中使用addIdentificationEntry(withNextSequentialPhoneNumber:label :)方法.

To provide identifying information about incoming callers, you use the addIdentificationEntry(withNextSequentialPhoneNumber:label:) method in the implementation of beginRequest(with:).

class CustomCallDirectoryProvider: CXCallDirectoryProvider {
    override func beginRequest(with context: CXCallDirectoryExtensionContext) {
        let labelsKeyedByPhoneNumber: [CXCallDirectoryPhoneNumber: String] = [ … ]
        for (phoneNumber, label) in labelsKeyedByPhoneNumber.sorted(by: <) {
            context.addIdentificationEntry(withNextSequentialPhoneNumber: phoneNumber, label: label)        
        }

        context.completeRequest()
    }
}

现在是个坏消息:就Iphone而言,据我所知(2021年),如果开发者应用程序不属于用户联系或社交网络的一部分,则无法访问其传入的电话号码,这无疑是值得的改变.

Now the bad news: For Iphone, as far as I know (2021), developers apps do not have access to the incoming phone number if its not part of the user's contact or social network, this is arguably something that would deserve to change.

也许您可能会做一些诸如后台处理之类的事情,在接听电话时进行截屏,然后处理图像以提取电话号码.但是我对IOS的经验很少,所以我什至不知道苹果是否会允许这样做.无论如何,祝你好运,如果成功的话,不要犹豫发布一个软件包,那绝对是一个很好的选择!

Maybe you could potentially do something like a background process that takes a screen capture when you receive a call, and then process the image to extract the phone number. But I have very little experience with IOS so I don't even know if Apple would allow that. Anyways good luck and don't hesite to publish a package if you are successful with that, it would be definitely a great one!

最后,您必须通过鸽子:

Finally you have to implement typesafe platform channels via Pigeon:

import 'generated_pigeon.dart'

void onClick() async {
  SearchRequest request = SearchRequest()..query = 'test';
  Api api = Api();
  SearchReply reply = await api.search(request);
  print('reply: ${reply.result}');
}

使用Pigeon不需要在主机和客户端之间匹配消息名称和数据类型的字符串.它支持:嵌套类,将消息分组为API,生成异步包装器代码以及向任一方向发送消息.生成的代码是可读的,并保证在不同版本的多个客户端之间不会存在冲突.受支持的语言是Objective-C,Java,Kotlin和Swift(通过Objective-C互操作).

Using Pigeon eliminates the need to match strings between host and client for the names and datatypes of messages. It supports: nested classes, grouping messages into APIs, generation of asynchronous wrapper code and sending messages in either direction. The generated code is readable and guarantees there will be no conflicts between multiple clients of different versions. Supported languages are Objective-C, Java, Kotlin and Swift (via Objective-C interop).

这篇关于快速查看来电的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆