【问题标题】:How to allow a user to select one or more phone numbers and/or email addresses from a Contact in Android如何允许用户从 Android 中的联系人中选择一个或多个电话号码和/或电子邮件地址
【发布时间】:2018-06-04 21:56:00
【问题描述】:

我想让用户使用联系人选择器选择联系人,然后选择一个或多个电话号码和/或电子邮件地址,然后存储这些值。

以下适用于获取多个电话号码:

Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);

以下是多个电子邮件地址:

Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);

但使用 Contactables 会使应用程序崩溃:

Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Contactables.CONTENT_URI);

我不一定要获取联系人的所有电话号码或所有电子邮件,我希望用户选择一个(或多个,如果可能)。

【问题讨论】:

    标签: java android


    【解决方案1】:

    是的,这是可能的,选择一个联系人后,如果该联系人有多个电话号码,则会弹出一个对话框。此代码由 Howetti:Selecting a number from user with multiple numbers when using the contact picker 提供。确保将此权限添加到您的清单
    <uses-permission android:name="android.permission.INTERNET"/>.

    import android.Manifest;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Build;
    import android.provider.ContactsContract;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    
    public class MainActivity extends AppCompatActivity {
        public static final String TAG= "YOUR-TAG-NAME";
        private static final int REQUEST_PICK_CONTACT= 100;
        private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, REQUEST_PICK_CONTACT);
            SetpermissionReadContacts();
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            Uri result = data.getData();
            Log.v(TAG, "Got a result: " + result.toString());
            Cursor c;
    
    // get the contact id from the Uri
            String id = result.getLastPathSegment();
    
    // query for phone numbers for the selected contact id
            c = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
                    new String[]{id}, null);
    
            int phoneIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            int phoneType = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    
            if(c.getCount() > 1) { // contact has multiple phone numbers
                final CharSequence[] numbers = new CharSequence[c.getCount()];
                int i=0;
                if(c.moveToFirst()) {
                    while(!c.isAfterLast()) { // for each phone number, add it to the numbers array
                        String type = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(this.getResources(), c.getInt(phoneType), ""); // insert a type string in front of the number
                        String number = type + ": " + c.getString(phoneIdx);
                        numbers[i++] = number;
                        c.moveToNext();
                    }
                    // build and show a simple dialog that allows the user to select a number
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  //  builder.setTitle(R.string.select_contact_phone_number_and_type);
                    builder.setItems(numbers, new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface dialog, int item) {
                            String number = (String) numbers[item];
                            int index = number.indexOf(":");
                            number = number.substring(index + 2);
                           // loadContactInfo(number); // do something with the selected number
                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.setOwnerActivity(this);
                    alert.show();
    
                } else Log.w(TAG, "No results");
            } else if(c.getCount() == 1) {
                // contact has a single phone number, so there's no need to display a second dialog
            }
    
        }
    
    
        private void SetpermissionReadContacts() {
            // Check the SDK version and whether the permission is already granted or not.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
                //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
            }
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-23
      • 1970-01-01
      • 2014-02-12
      • 2011-06-04
      • 2013-02-11
      • 2020-03-01
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      相关资源
      最近更新 更多