dart 如何在flutter中打开android平台上的添加联系人页面?

xxhby3vn  于 2023-03-10  发布在  Flutter
关注(0)|答案(3)|浏览(239)

我正在使用flutter制作一个应用程序,它使用用户手机中的联系人(使用contact_services插件)。因此,它也需要给予使用系统默认的添加联系人页面添加新联系人的功能。然而,contact_services中没有这样的功能。使用插件或现有的contact_services插件打开添加新联系人页面的方法是什么?

void openAddContacts() async{
if (Platform.isAndroid) {
  final AndroidIntent intent = AndroidIntent(
    action: 'ContactsContract.Intents.Insert.ACTION',
    category: 'ContactsContract.RawContacts.CONTENT_TYPE',
  );
  await intent.launch();
 }
}
2skhul33

2skhul331#

这可能会帮助您:https://github.com/differenz-system/Addressbook.Flutter或者,如果您想深入了解其背后的机制,我建议您使用Platform Channel与本地代码进行通信。下面是Google为地址簿提出的一个示例:https://developers.google.com/protocol-buffers/docs/darttutorial

gab6jxml

gab6jxml2#

我找到了解决方案。基本上你必须从MainActivity.kt调用方法,我们将使用Intent打开默认的联系人应用,就像WhatsApp所做的那样。
首先,在.dart文件上创建将连接到MainActivity.kt的通道,如下所示:
static const openCreateContact = MethodChannel('com.example.yourApp/openCreateContact');
还要创建一个函数来调用我们稍后将在MainActivity.kt中实现的方法,然后可以在onPressed()上调用它:

onPressed: () async {
 String phone= "+212 666 000 111";
 String name = "John Doe";
await openCreateContactMethod(name , phone);
},

Future openCreateContactMethod(name, phone) async {
await openCreateContact.invokeMethod('openCreateContact', {
  'name': name,
  'phone': phone,
});

}
然后,转到MainActivity.kt,它看起来将是这样的:

package com.example.yourApp

import android.content.Intent
import android.provider.ContactsContract
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
private val channel = "com.example.yourApp/openCreateContact";

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger,channel).setMethodCallHandler{
            call, result ->

         
            if (call.method == "openCreateContact") {
                val name = call.argument<String>("name")
                val phone = call.argument<String>("phone")
                openCreateContact(name!!, phone!!)
                result.success(true)
            } else {
                result.notImplemented()
            }
        }

    }

    fun openCreateContact(name: String, phone: String) {
        // Create a new Intent to open the Contacts app with a pre-filled contact form
        val intent = Intent(Intent.ACTION_INSERT)
        intent.type = ContactsContract.Contacts.CONTENT_TYPE

        // Set the contact fields using the data provided by the Flutter app
        intent.putExtra(ContactsContract.Intents.Insert.NAME, name)
        intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone)

        // Launch the Contacts app with the pre-filled contact form
        startActivity(intent)
    }
}
qhhrdooz

qhhrdooz3#

如果你使用这个插件https://pub.dev/packages/contacts_service,你可以在这里查看完整工作示例https://github.com/lukasgit/flutter_contacts/blob/master/example/lib/main.dart
添加权限
Android编号
将以下权限添加到AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_CONTACTS" />  
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

iOS编号
在Info.plist文件中设置NSContactsUsageDescription

<key>NSContactsUsageDescription</key>  
<string>This app requires contacts access to function properly.</string>

之后,您可以通过上面的链接使用示例中的代码。

相关问题