如何显示我的联系电话号码并获取其中一个电话号码以在我的应用程序上使用?

发布于 2024-10-05 02:25:03 字数 92 浏览 2 评论 0原文

我有一个应用程序必须显示我的手机联系人列表。用户必须选择一个电话号码,而我必须在我的应用程序上以编程方式使用该电话号码。 我该怎么做呢?

代码示例会很棒。

I have an app that has to show my phone contact list. The user has to select one phone number and I have to use this phone number programmatically on my app.
How can I do it?

Code examples will be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坚持沉默 2024-10-12 02:25:03

只需将一个按钮连接到 onBrowseForNumbersButtonClicked() 方法...将代码放在 formattedPhoneNumber 行下面...然后就可以开始了。

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.View;

public class TestActivity extends Activity {

    private static final int REQUEST_CONTACT_NUMBER = 123456789;

    /** Pops the "select phone number" window */
    public void onBrowseForNumbersButtonClicked(View view) {
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);
        startActivityForResult(contactPickerIntent, REQUEST_CONTACT_NUMBER);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if(data != null && requestCode == REQUEST_CONTACT_NUMBER) {  
                Uri uriOfPhoneNumberRecord = data.getData();
                String idOfPhoneRecord = uriOfPhoneNumberRecord.getLastPathSegment();
                Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, Phone._ID + "=?", new String[]{idOfPhoneRecord}, null);
                if(cursor != null) {
                        if(cursor.getCount() > 0) {
                            cursor.moveToFirst();
                            String formattedPhoneNumber = cursor.getString( cursor.getColumnIndex(Phone.NUMBER) );
                            Log.d("TestActivity", String.format("The selected phone number is: %s", formattedPhoneNumber));
                        }
                        cursor.close();
                }
            }
            else {
                Log.w("TestActivity", "WARNING: Corrupted request response");
            }
        }
        else if (resultCode == RESULT_CANCELED) {
            Log.i("TestActivity", "Popup canceled by user."); 
        }
        else {
            Log.w("TestActivity", "WARNING: Unknown resultCode");
        }
    }
}

Just wire up a button to the onBrowseForNumbersButtonClicked() method... drop your code in underneath the formattedPhoneNumber line... and you're good to go.

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.View;

public class TestActivity extends Activity {

    private static final int REQUEST_CONTACT_NUMBER = 123456789;

    /** Pops the "select phone number" window */
    public void onBrowseForNumbersButtonClicked(View view) {
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);
        startActivityForResult(contactPickerIntent, REQUEST_CONTACT_NUMBER);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if(data != null && requestCode == REQUEST_CONTACT_NUMBER) {  
                Uri uriOfPhoneNumberRecord = data.getData();
                String idOfPhoneRecord = uriOfPhoneNumberRecord.getLastPathSegment();
                Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, Phone._ID + "=?", new String[]{idOfPhoneRecord}, null);
                if(cursor != null) {
                        if(cursor.getCount() > 0) {
                            cursor.moveToFirst();
                            String formattedPhoneNumber = cursor.getString( cursor.getColumnIndex(Phone.NUMBER) );
                            Log.d("TestActivity", String.format("The selected phone number is: %s", formattedPhoneNumber));
                        }
                        cursor.close();
                }
            }
            else {
                Log.w("TestActivity", "WARNING: Corrupted request response");
            }
        }
        else if (resultCode == RESULT_CANCELED) {
            Log.i("TestActivity", "Popup canceled by user."); 
        }
        else {
            Log.w("TestActivity", "WARNING: Unknown resultCode");
        }
    }
}
半步萧音过轻尘 2024-10-12 02:25:03

您需要将联系人选择器与从给定联系人检索电话号码结合起来。

查看选择联系人的要点以及如何读取联系人数据

You need to combine a contact picker, with retrival of phone number from a given contact.

Check this Essentials to pick a contact and how to read contact data

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文