如何分享联系方式

问题描述 投票:0回答:3

我有一个需要手机验证的应用程序。身份验证后,我想设置一个功能,可以与人们分享其他联系人。

例如,我是A,在我的联系人中,3个人使用我的应用程序名为B,C和D.假设我想将B的联系人分享给D.我该怎么做?

与Whatsapp上的分享联系人相同。

搜索了很多但没有得到如何实现这一点。

java android share
3个回答
0
投票

实现这一目标的一种方法是通过Intent。您可以将数据存储在Intent中并通过它传递给另一个Screen

Intent intent = getIntent();

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

String id = intent.getStringExtra();

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


0
投票

试试这个:

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


0
投票

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

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>
© www.soinside.com 2019 - 2024. All rights reserved.