代码
Intent inten = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(inten,PICK_CONTACT);
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
Cursor c;
switch (reqCode) {
case (PICK_CONTACT):
Uri result = data.getData();
Log.v("TAG", "Got a result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for phone numbers for the selected contact id
c = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
int phoneIdx = c.getColumnIndex(Phone.NUMBER);
int phoneType = c.getColumnIndex(Phone.TYPE);
if(c.getCount() > 1) { // contact has multiple phone numbers
final CharSequence[] numbers = new CharSequence[c.getCount()];
int i=0;
if(c.moveToFirst()) {
while(!c.isAfterLast()) { // for each phone number, add it to the numbers array
String type = (String) Phone.getTypeLabel(this.getResources(), c.getInt(phoneType), ""); // insert a type string in front of the number
String number = type + ": " + c.getString(phoneIdx);
numbers[i++] = number;
c.moveToNext();
}
// build and show a simple dialog that allows the user to select a number
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Number");
builder.setItems(numbers, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
String number = (String) numbers[item];
int index = number.indexOf(":");
number = number.substring(index + 2);
cNumber=number;
edt.setText(cNumber);
//Toast.makeText(getApplicationContext(), number, Toast.LENGTH_LONG).show();
// loadContactInfo(number); // do something with the selected number
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity(this);
alert.show();
} else Log.w("TAG", "No results");
} else if(c.getCount() == 1) {
// contact has a single phone number, so there's no need to display a second dialog
if(c.moveToFirst())
{
cNumber = c.getString(c.getColumnIndex("data1"));
edt.setText(cNumber);
//Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_LONG).show();
}
}
break;
}
}
在我的活动中,我也写回了... ...
@Override
public void onBackPressed() {
btnflag++;
if(btnflag==1)
{
Intent mainscr = new Intent(FindList1.this, MainScreen.class);
startActivity(mainscr);
btnflag=0;
finish();
}
}
但不幸的是坠机已经发生.....
答案 0 :(得分:1)
当用户点击后退按钮时,它将调用activityResult()。我猜你在这段代码中得到了空指针异常。
Uri result = data.getData();
Log.v("TAG", "Got a result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for phone numbers for the selected contact id
c = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
int phoneIdx = c.getColumnIndex(Phone.NUMBER);
int phoneType = c.getColumnIndex(Phone.TYPE);
data.getData()它将返回null。因为您未在“联系人”活动中选择任何联系人和后退按钮。
if(data==null){
return;
}
并处理异常
onBackPressed()方法将在当前活动被按下时调用按钮。你在其他活动中不会打电话。