簡體   English   中英

如何從所選聯系人中提取電話號碼?

[英]How to extract phone number from the selected contact?

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中,並且電話號碼將存儲在sharedpreferences中。實現嗎? 謝謝

你有沒有嘗試過?

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();

上面的代碼僅供參考。

以下鏈接的可能重復項

從Android聯系人選擇器獲取聯系信息

請參考上面的鏈接。 已得到詳細解答。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM