首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

电话接听并查看此人姓名

2012-08-24 
电话接听并查看该人姓名application? .....? receiver android:name.ServiceReceiver??? intent-f

电话接听并查看该人姓名

<application>
? .....
? <receiver android:name=".ServiceReceiver">
??? <intent-filter>
????? <action android:name="android.intent.action.PHONE_STATE" />
??? </intent-filter>
? </receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>

?

public class ServiceReceiver extends BroadcastReceiver {
? @Override
? public void onReceive(Context context, Intent intent) {
??? MyPhoneStateListener phoneListener=new MyPhoneStateListener();
??? TelephonyManager telephony = (TelephonyManager)
???????????????????? context.getSystemService(Context.TELEPHONY_SERVICE);
??? telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
? }
}

?

?

public class MyPhoneStateListener extends PhoneStateListener {
?? private String[] projection = new String[] {
??????????? People._ID, People.NAME, People.NUMBER
??? };
? public void onCallStateChanged(int state,String incomingNumber){
??? switch(state)
??? {
????? case TelephonyManager.CALL_STATE_IDLE:
??????? Log.d("DEBUG", "IDLE");
????? break;
????? case TelephonyManager.CALL_STATE_OFFHOOK:
??????? if(!incomingNumber.equals("")){
????????? handleCall(incomingCall);
??????? }
????? break;
???? case TelephonyManager.CALL_STATE_RINGING:
??????? Log.d("DEBUG", "RINGING");
???? break;
??? }
? }
? public void handleCall(String incomingCall){
??? Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
???????????????????????? incomingNumber);
??? contactsCursor = context.getContentResolver().query(contactUri,
?????????????????????? projection, null , null, People.NAME + " ASC");
??? if(contactsCursor.moveToFirst()){
????? int phoneNameIndex = contactsCursor.getColumnIndex(People.NAME);
????? String phoneNameStr = contactsCursor.getString(phoneNameIndex);
????? Log.d("DEBUG",phoneNameStr + " is calling");
??? }else{
????? Log.d("DEBUG","Not a contact");
??? }
? }
}

热点排行