Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javaandroid如何使用联系人选择器_Java_Android_Android Intent_Android Contacts - Fatal编程技术网

javaandroid如何使用联系人选择器

javaandroid如何使用联系人选择器,java,android,android-intent,android-contacts,Java,Android,Android Intent,Android Contacts,我编程一个应用程序发送短信的协会,我需要你的帮助。。。 我想通过联系人选取器从联系人中检索手机号码。我无法检索用户选择的结果 我的代码: public class MainActivity extends Activity { private static final int CONTACT_PICKER_RESULT = 0; private Button contacts = null; private Button envoyer = null; priva

我编程一个应用程序发送短信的协会,我需要你的帮助。。。 我想通过联系人选取器从联系人中检索手机号码。我无法检索用户选择的结果

我的代码:

public class MainActivity extends Activity {
    private static final int CONTACT_PICKER_RESULT = 0;
    private Button contacts = null;
    private Button envoyer = null;
    private TextView mobile = null;
    private int PICK_CONTACT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Elements of th activity
        contacts = (Button) findViewById(R.id.contacts);
        envoyer = (Button) findViewById(R.id.envoyer);
        mobile = (TextView) findViewById(R.id.mobile);



        contacts.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);  
                startActivityForResult(contactPickerIntent, 1001); 
            }
        });

        envoyer.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {



            }
        });

    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
      // How to retrieve the mobile and the name of the contact selected ?
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



    public void open_contacts(){


    }


}

非常感谢你

使用权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />
doLaunchContactPicker(new View(mContext));

public void doLaunchContactPicker(View view)
{
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
Uri result = data.getData();
String id = result.getLastPathSegment();

cursor = getContentResolver().query(Phone.CONTENT_URI, null,
    Phone.CONTACT_ID + " = ? AND " + Phone.TYPE + " = ? ",
    new String[] { id, String.valueOf(Phone.TYPE_WORK) }, null);

phoneIdx = cursor.getColumnIndex(Phone.NUMBER);
if (cursor.moveToFirst())
{
    phone = cursor.getString(phoneIdx);
}
onActivityResult:

<uses-permission android:name="android.permission.READ_CONTACTS" />
doLaunchContactPicker(new View(mContext));

public void doLaunchContactPicker(View view)
{
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
Uri result = data.getData();
String id = result.getLastPathSegment();

cursor = getContentResolver().query(Phone.CONTENT_URI, null,
    Phone.CONTACT_ID + " = ? AND " + Phone.TYPE + " = ? ",
    new String[] { id, String.valueOf(Phone.TYPE_WORK) }, null);

phoneIdx = cursor.getColumnIndex(Phone.NUMBER);
if (cursor.moveToFirst())
{
    phone = cursor.getString(phoneIdx);
}

您可以使用上面的光标检索针对所选联系人存储的任何号码。

这不起作用。。。我没有错误,但如果(cursor.moveToFirst())总是错误的…请使用以下命令:cursor=getContentResolver().query(Phone.CONTENT\u URI,null,null,null);