0
点赞
收藏
分享

微信扫一扫

判断来电状态 确定号码:

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"); 

 } 

 } 

}



如果要使用receive则:

<application> 

 ..... 

 <receiver android:name=".ServiceReceiver"> 

 <intent-filter> 

 <action android:name="android.intent.action.PHONE_STATE" /> 

 </intent-filter> 

 </receiver> 


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); 

 } 

} 

</application> 

<uses-permission android:name="android.permission.READ_PHONE_STATE"> 

</uses-permission>

举报

相关推荐

0 条评论