<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");
}
}
}
电话接听并查看该人姓名
原创mb649166f4c151e 博主文章分类:android ©著作权
文章标签 Android android ide 文章分类 JavaScript 前端开发
下一篇:输入法的关闭和打开
-
linphone 自动接听电话
自动接听电话,代码分析,流程分析,思路过程
linphone 自动接听 ide xml php