- 我在我的活动中使用了选择联系人功能。它可以很好地选择联系人和其他详细信息,如姓名。
- 但是当我单击按钮时,它会显示联系意图并显示数字一切都很好。
- 但是,如果我没有选择数字并单击后按意味着应用程序强制关闭。
代码
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();
}
}
但不幸的是,事故发生了......