How to pass Uri value to bundle value
This following code fetches multiple contacts and stores them in a bundle:
Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
phonebookIntent.putExtra("additional", "phone-multi");
phonebookIntent.putExtra("maxRecipientCount", MAX_PICK_CONTACT);
phonebookIntent.putExtra("FromMMS", true);
startActivityForResult(phonebookIntent, REQUEST_CODE_PICK_CONTACT);
The code below retrieves the selected data:
if (requestCode == REQUEST_CODE_PICK_CONTACT) {
Uri contractData = data.getData();
Bundle contantData = data.getExtras();
String result= contantData.getString("result");
ArrayList<String> contacts = contantData.getStringArrayList("result");
ContactRetriever cr = new ContactRetriever(getApplicationContext(), contacts);
Person p = cr.getPerson();
I want the data in Uri so that I can make use of it in other classes which refer to Uri data.
How can I pass the bundle value to the Uri data?
Another example of using Uri data as "contactData":
private String getNumber() {
String ret = null;
Cursor cId = cr.query(contractData, new String[]{ContactsContract.Contacts._ID}, null, null, null);
if (cId.moveToFirst()){
id = cId.getString(cId.getColumnIndex(ContactsContract.Contacts._ID));
Log.i(TAG + " IDs: ", id);
}
cId.close();
Cursor cNum = cr.query(contractData, null, null, null, null);
if (cNum.moveToNext()){
ret = cNum.getString(cNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(ret, TAG + " NUMBERS: ");
}
return ret;
}
private String getName() {
String ret = null;
Cursor c = cr.query(contractData, null, null, null, null);
if (c.moveToFirst())
ret = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
c.close();
Log.i(TAG + "NAMES: ", ret);
return ret;
}
Specific usage of contractData examples:
Cursor cId = cr.query(contractData, new String[]{ContactsContract.Contacts._ID}, null, null, null);
Cursor cNum = cr.query(contractData, null, null, null, null);
Cursor c = cr.query(contractData, null, null, null, null);
You can pass the Uri as a string using the toString
function, then parse it back as a Uri.
So, to send it as part of the URI:
phonebookIntent.putExtra("uri", uriData.toString());
To read it back:
Uri uriData = Uri.parse(extras.getString"uri"));
Uri implements parceleable so you cant simply pass intent.putExtra() instead you have to use it this way:
phonebookIntent.putParceleableExtra("uri", your_uri_object);
read it back:
Uri uri = getIntent().getParcelableExtra("uri");
链接地址: http://www.djcxy.com/p/31988.html
下一篇: 如何通过Uri值来捆绑价值