0
私は自分のアプリから連絡先にアクセスしましたが、2つのテキストビューを共有設定に保存する方法を知っています。コードは、それが共有のprefsに格納することができないという点を除いて正常に動作しています。sharedprefsにアクセスした後に連絡先を保存する
contact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pickContact(null);
String a = tvCname.getText().toString();
String b = tvCnum.getText().toString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Name",a);
editor.putString("Phone Number",b);
editor.commit();
Toast.makeText(MainActivity.this,"added",Toast.LENGTH_LONG).show();
}
});
これは、電話機の連絡先にアクセスするためのコードです。
public void pickContact(View v)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple startActivityForResult
switch (requestCode) {
case RESULT_PICK_CONTACT:
contactPicked(data);
//test
break;
case 99:
Intent intentReceived = data;
String strName = intentReceived.getStringExtra("name");
String strBT = intentReceived.getStringExtra("bloodType");
String strNum = intentReceived.getStringExtra("number");
String strAddress = intentReceived.getStringExtra("address");
String strOther = intentReceived.getStringExtra("other");
tvName.setText(strName);
tvBg.setText(strBT);
tvNum.setText(strNum);
tvAddress.setText(strAddress);
tvOther.setText(strOther);
Toast.makeText(this,"Profile added",Toast.LENGTH_LONG).show();
break;
}
} else {
Log.e("MainActivity", "Failed to pick contact");
}
}
/**
* Query the Uri and read contact details. Handle the picked contact data.
* @param data
*/
private void contactPicked(Intent data) {
Cursor cursor = null;
try {
String phoneNo = null ;
String cName = null;
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
cName = cursor.getString(nameIndex);
// Set the value to the textviews
tvCname.setText(phoneNo);
tvCnum.setText(cName);
} catch (Exception e) {
e.printStackTrace();
}
}
この値を取得するにはSharedPreferences
に値を保存するには? – telejele
SharedPreferences prefs = getSharedPreferences( "prefidentifier"、Context.MODE_PRIVATE); エディタエディタ= prefs.edit(); editor.putString( "Name"、a); editor.putString( "電話番号"、b); editor.commit(); このコードは値を保存するために使用します。 SharedPreferences prefs = getSharedPreferences( "prefidentifier"、Context.MODE_PRIVATE); 文字列name = sharedpreferences.getString( "Name"、null); 文字列phone = sharedpreferences.getString( "Phone Number"、null); このコードは、値を返すために使用します。 –