HERE MY CODE ..对于任何传入号码,我尝试找到来电者姓名并返回此方法调用的名称..请帮助
public String getName(String number){
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(number));
String name = "Incoming call from";
ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri,new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
try{
if(contactLookup != null && contactLookup.getCount()>0 ){
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
}
finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return name;
}
这里方法调用部分
public void onCallStateChanged(int state, String incomingNumber) {
String s = incomingNumber;
String ss = getName(s);
if(state== TelephonyManager.CALL_STATE_RINGING ){
tts.speak(ss +" calling please answer ", TextToSpeech.QUEUE_FLUSH, null);
Toast.makeText(getApplicationContext(), "Phone is Ringing : " + incomingNumber, Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
请使用PhoneStateListener。它有一个onCallStateChanged处理程序;您将获得的一个提供的参数是一个包含传入电话号码的字符串。
检查问题here以获取有关实施的更多详细信息。