gpt4 book ai didi

java - Android 联系电话、地址和姓名 Intent

转载 作者:行者123 更新时间:2023-12-01 11:35:12 25 4
gpt4 key购买 nike

我正在使用基本 Intent 在 Android Studio 中获取联系人。选择联系人后,我想提取地址(家庭)、电话和姓名。我目前只能获取姓名/地址或姓名/电话。由于我相信的 Intent 类型,我无法获得全部 3 个。

问题始于 addDeliveryItem() 的 Intent 。根据 Intent 类型,我可以提取电话号码或地址。我两个都需要!!!

我注意到一些奇怪的事情:我的 getContactIDFromURI() 根据 Intent 类型返回不同的联系人 ID。

我对这篇文章进行了重大编辑,希望能找到答案。我现在已经包含了整个 .java 文件,以防您发现任何问题。我快要疯了,我会向任何能解决这个问题的人致以深深的爱:(。

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

//our main super list of deliveries
ArrayList<String> deliveryListItems = new ArrayList<String>();

//string adaptor that will handle data of listview
MyCustomAdapter adapter;

//clickCount
int clickCounter = 0;

//this is required to pick a contact for some reason.
private static int PICK_CONTACT = 1;

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

//initialize our custom adapter for custom list
adapter = new MyCustomAdapter(deliveryListItems, this);

ListView lView = (ListView)findViewById(android.R.id.list);
lView.setAdapter(adapter);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

/* Begin custom Deliverance functions */
public void addDeliveryItem(View v){
//these 3-4 lines create an intent to pick contacts. INTENT TYPE IS DRIVING ME CRAZY
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
//intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}

//this function handles receiving contact numbers after they have been picked with intent
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

//URI to store the contact URI - basically URL to contact
Uri uriContact;
String contactName = null;
String contactPhone = null;
String contactAddress = null;
String contactID = null;

//successful contact lookup like a boss
if ((requestCode == PICK_CONTACT) && (resultCode == RESULT_OK)) {
//store contact URI
uriContact = data.getData();

contactName = getContactNameFromURI(uriContact);
contactPhone = getContactPhoneFromURI(uriContact);
contactID = getContactIdFromURI(uriContact);
contactAddress = getContactAddressFromURI(uriContact);

//add result to list
deliveryListItems.add(contactName);
deliveryListItems.add(contactPhone);
deliveryListItems.add(contactAddress);
deliveryListItems.add(contactID);

adapter.notifyDataSetChanged();
clickCounter++;
}
}

public String getContactIdFromURI(Uri uri){
String contactID = null;

// getting contacts "ID" that we need for stuff
Cursor cursorID = getContentResolver().query(uri,
new String[]{ContactsContract.Contacts._ID},
null, null, null);

if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();

return contactID;
}

public String getContactNameFromURI(Uri uri){
String contactName = null;
// querying contact data store
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst()) {
// DISPLAY_NAME = The display name for the contact.
// HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number.
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}

cursor.close();
return contactName;
}

public String getContactPhoneFromURI(Uri uri){
String contactNumber = null;
String contactID = getContactIdFromURI(uri);

// Using the contact ID now we will get contact phone number
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

new String[]{contactID},
null);

if (cursor.moveToFirst()) {
contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}

cursor.close();
return contactNumber;
}

public String getContactAddressFromURI(Uri uri){
String rAddress = null;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst()) {
try {
rAddress = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
}catch(Exception e){
return "nope";
}
}

cursor.close();
return rAddress;
}
}

我可以设置 Intent 类型以便能够检索我需要的所有数据吗?我显然不想再次选择联系人。

如果这是一个简单的问题,我很抱歉。我对 Android 开发还是个新手:)。

编辑:这是我用于检索给定 URI 的联系电话、姓名和地址的函数。它们都适用于不同的 Intent 类型。

最佳答案

正如 @Shaik 提到的,您可以尝试此方法来获取联系 ID

public static int getContactIDFromNumber(String contactNumber,Context context)
{
contactNumber = Uri.encode(contactNumber);
int phoneContactID = new Random().nextInt();
Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.contactNumber),new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
while(contactLookupCursor.moveToNext()){
phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
contactLookupCursor.close();

return phoneContactID;
}

关于java - Android 联系电话、地址和姓名 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30068553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com