如何分享联系方式

How to share contact

我有一个应用程序需要 phone 身份验证才能使用。 身份验证后,我想设置一个功能,可以将其他联系人分享给其他人。

比如我是A,在我的联系人中有3个人使用我的应用程序,名为B、C和D。 假设我想把 B 的联系方式分享给 D。我该怎么做?

与在 Whatsapp 上共享联系人相同。

搜索了很多但没有找到实现方法。

实现这一目标的一种方法是通过 Intent。您可以将数据存储在 Intent 中,然后通过

将其传递给另一个 Screen
Intent intent = getIntent();

然后在另一个屏幕中通过以下方式检索相同的意图:

String id = intent.getStringExtra();

此外,您必须获得用户许可才能访问联系人,然后通过意图传递联系人。需要在Manifest Files中添加权限。

试试这个:

private void shareContact(String[] args) {

    String lookupKey = null;
    Cursor cur = getContentResolver().query(Contacts.CONTENT_URI, new String[] { Contacts.LOOKUP_KEY }, Contacts._ID + " = " + contactId, null, null);
    if (cur.moveToFirst()) {
        String lookupKey = cur.getString(0);
    }
    if(lookupKey!=null){
        Uri vcardUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType(ContactsContract.Contacts.CONTENT_VCARD_TYPE);
        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
        intent.putExtra(Intent.EXTRA_SUBJECT, "Contact Name"); 
        startActivity(intent);
    }
}

更多详情: https://developer.android.com/reference/android/provider/ContactsContract.Contacts#CONTENT_VCARD_URI

试试这个:我相信它会有帮助

setContentView(R.layout.main);

contactNumber = (TextView)findViewById(R.id.contactnumber);

Button buttonPickContact = (Button)findViewById(R.id.pickcontact);
buttonPickContact.setOnClickListener(new Button.OnClickListener(){

 @Override
 public void onClick(View arg0) {
// TODO Auto-generated method stub


Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);             


 }});
}

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 // TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == RQS_PICK_CONTACT){
if(resultCode == RESULT_OK){
Uri contactData = data.getData();
Cursor cursor =  managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();

  String number =       cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

  //contactName.setText(name);
  contactNumber.setText(number);
  //contactEmail.setText(email);
       }
     }
   }
 }

以下是相同的 XML:

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

  <Button
    android:id="@+id/pickcontact"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Pick Contact" />

  <TextView
   android:id="@+id/contactnumber"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" />

</LinearLayout>