我有一个应用程序,需要电话认证才能使用。认证后,我想设置一个功能,它将能够与人共享其他联系人。
例如,我是A,在我的联系人中有3人使用我的应用程序B,C和D。假设我想和D分享B的联系方式。
就像在Whatsapp上分享联系一样。
搜索了相当多,但没有得到如何实现这一点。
发布于 2018-08-30 04:04:08
试试这个:我相信它会对有帮助
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>
发布于 2018-08-29 12:30:06
实现这一目标的一种方法是通过意图。您可以将数据存储在意图中,并将其传递给另一个屏幕。
Intent intent = getIntent();
然后在另一个屏幕上通过以下方法检索相同的意图:
String id = intent.getStringExtra();
此外,您还必须获得用户的许可才能访问联系人,然后通过意图传递联系人。权限需要添加到报表文件中。
发布于 2018-08-29 12:37:24
试试这个:
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);
}
}
更多细节:URI
https://stackoverflow.com/questions/52077218
复制相似问题