gpt4 book ai didi

java - 如何从选定的联系人中提取电话号码?

转载 作者:行者123 更新时间:2023-12-01 15:22:50 24 4
gpt4 key购买 nike

public class ImportContactsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button pickContact = (Button) findViewById(R.id.contacts);
pickContact.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
});
}

public void onActivityResult(int reqCode, int resultCode, Intent data) {

super.onActivityResult(reqCode, resultCode, data);

switch (reqCode) {
case (1) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);

if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
TextView contactView = (TextView) findViewById(R.id.contactView);
contactView.setText(name.toString());
}
}
break;
}
}

我正在开发一个Android应用程序,我正在将手机联系人导入到我的应用程序中,用户单击所选联系人后,该联系人将显示在TextView中,并且电话号码将存储在共享首选项中...我知道如何实现吗?谢谢

最佳答案

你尝试过吗?

Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(People.NUMBER));
String email = cursor.getString(cursor.getColumnIndexOrThrow(People.PRIMARY_EMAIL_ID));
contactName.setText(name);
contactNumber.setText(number);
contactEmail.setText(email);

适用于 SharedPreferences 的商店..

  // We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("phonenumber", number);

// Commit the edits!
editor.commit();

以上代码仅供理解..

关于java - 如何从选定的联系人中提取电话号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10634332/

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