0
我正在開發一個使用firebase(用於後端)的簡單信使應用程序。我希望將用戶的聯繫電話號碼與存儲在我的數據庫中的聯繫人電話號碼進行比較,以便用戶可以查看精緻的聯繫人列表。我在谷歌查找,但我找不到一個方法來獲取數字存儲在一個單獨的變量。我想下面的代碼在堆棧溢出另一篇文章,但我在getactivity得到一個錯誤說如果你正在使用的代碼中的活動我覺得比getActivity()不是「無法解析法」從用戶手機獲取聯繫人
ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
switch (phoneType) {
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
Log.e(name + "(mobile number)", phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
Log.e(name + "(home number)", phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
Log.e(name + "(work number)", phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
Log.e(name + "(other number)", phoneNumber);
break;
default:
break;
}
}
pCur.close();
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
感謝您對穆克什答覆...我會嘗試一下代碼,我會給你我的反饋:) – ananymous59 2014-11-25 13:14:13
繼續,並取得成功:) – 2014-11-25 13:19:54
感謝百萬工作:) – ananymous59 2014-11-25 13:51:00