我有这段代码可以将声音作为联系人,但是当我插入第二种情况时,如果有重复的想法,java会认出我吗?我简化了代码,以免事先造成混淆:
码:
static final int PICK_CONTACT1 = 1;
static final int PICK_CONTACT2 = 1;
// first Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT1);
// second
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT2);
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT1):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) { //ecc...
case (PICK_CONTACT2):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id = c
.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
最佳答案
您的PICK_CONTACT1
和PICK_CONTACT2
都等于1:
static final int PICK_CONTACT1 = 1;
static final int PICK_CONTACT2 = 1;
因此,您实际上在做:
switch(reqCode) {
case 1:
//stuff
case 1:
//stuff
}
您需要设定这些不同的值。另外,请确保在每种情况的末尾添加
break
。