【问题标题】:Unreliable result for checking incoming number in contact检查联系人中的来电号码的结果不可靠
【发布时间】:2017-03-16 16:16:28
【问题描述】:

我正在使用此处列出的代码Check Incoming number is stored in Contacts list or not android 来检查联系人中是否存在传入号码。此代码并不总是给出正确的结果。

在这个或其他更好的检查方法中是否需要一些更正?


代码:

String res = null;
try {
    ContentResolver resolver = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    String a = uri.getLastPathSegment();
    Cursor c = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, ContactsContract.CommonDataKinds.Phone._ID + "=?", new String[]{a}, null);

    if (c != null) { // cursor not null means number is found contactsTable
        if (c.getCount() > 0) {
        if (c.moveToFirst()) {   // so now find the contact Name
            res = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            //res = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        }
        c.close();
        }
    }
} catch (Exception ex) {
    ex.printStackTrace();
}
return res;

【问题讨论】:

  • "此代码并不总是给出正确的结果。"那么为什么不提供一些细节呢?解释为什么您还需要发布重复的问题。
  • 为什么你捕捉到异常并忽略它,这样你将永远得到准确的结果。
  • @Panache 你不知道他是怎么赚到5962分的这需要一两年多的努力,不要怪别人浪费时间对你来说,要有礼貌并检查以下答案。

标签: java android


【解决方案1】:

试试下面的代码,它很简单,对我有用。

public class TestActivity extends Activity {

private static final int REQUEST_CONTACT_NUMBER = 8512885487;

/** Pops the "select phone number" window */
public void onBrowseForNumbersButtonClicked(View view) {
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, REQUEST_CONTACT_NUMBER);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
    if(data != null && requestCode == REQUEST_CONTACT_NUMBER) {  
    Uri uriOfPhoneNumberRecord = data.getData();
    String idOfPhoneRecord = uriOfPhoneNumberRecord.getLastPathSegment();
    Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, Phone._ID + "=?", new String[]{idOfPhoneRecord}, null);
    if(cursor != null) {
    if(cursor.getCount() > 0) {
        cursor.moveToFirst();
        String formattedPhoneNumber = cursor.getString( cursor.getColumnIndex(Phone.NUMBER) );
        Log.d("TestActivity", String.format("The selected phone number is: %s", formattedPhoneNumber));
    }
    cursor.close();
    }
    }
else {
    Log.w("TestActivity", "WARNING: Corrupted request response");
}
}
else if (resultCode == RESULT_CANCELED) {
    Log.i("TestActivity", "Popup canceled by user."); 
}
else {
    Log.w("TestActivity", "WARNING: Unknown resultCode");
}
}
}

【讨论】:

  • 还是同样的问题,号码已保存但代码未找到。
  • @Panache 调试并检查问题所在,并帮助自己。
  • @Panache 如果您不想帮助自己,没有人可以帮助您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-23
  • 1970-01-01
相关资源
最近更新 更多