拨打电话
android.permission.CALL_PHONE,允许程序从非系统拨号器里输入电话号码
通话权限
android.permission.CALL_PRIVILEGED,允许程序拨打电话,替换系统的拨号器界面
读取电话状态
android.permission.READ_PHONE_STATE,访问电话状态
修改电话状态
android.permission.MODIFY_PHONE_STATE,修改电话状态,如飞行模式,但不包 替换系统拨号器界面
处理拨出电话
android.permission.PROCESS_OUTGOING_CALLS,允许程序监视,修改或放弃播出电话
AndroidManifest.xml
扩展阅读: android权限大全
----------------------------------------------------------------
Android在电话状态改变是会发送
action为android.intent.action.PHONE_STATE的广播
而拨打电话时会发送
action为android.intent.action.NEW_OUTGOING_CALL的广播
例如:
IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.NEW_OUTGOING_CALL"); filter.addAction("android.intent.action.PHONE_STATE");
-------------------------------------------------
去电时:
* 未接:phone_state=OFFHOOK;
* 挂断:phone_state=IDLE
* 来电时: *
* 未接:phone_state=RINGING
* 已接:phone_state=OFFHOOK;
* 挂断:phone_state=IDLE
-------------------------------------------
JAVA代码
public class PhoneCallReceiver extends BaseReceiver { /** //拨打电话判断及获取号码 if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){ String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); */ @Override public void onReceive(Context context, Intent intent) { String phone_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); /*去电时: * 未接:phone_state=OFFHOOK; * 挂断:phone_state=IDLE * 来电时: * * 未接:phone_state=RINGING * 已接:phone_state=OFFHOOK; * 挂断:phone_state=IDLE**/ System.out.println("phone_state======"+phone_state); String incoming_number = intent.getStringExtra("incoming_number"); //String incoming_number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER ); String outgoing_number = intent.getStringExtra("android.intent.extra.PHONE_NUMBER"); System.out.println("incoming_number======"+incoming_number); System.out.println("outgoing_number======"+outgoing_number); //去电设值 if(outgoing_number!=null && outgoing_number.length()>0){ System.out.println("去电号码"+outgoing_number); } //来电设值 if(incoming_number!=null && incoming_number.length()>0){ System.out.println("来电号码"+incoming_number); } if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(phone_state) || TelephonyManager.EXTRA_STATE_RINGING.equals(phone_state)) { System.out.println("去电话未接或来电未接及来电已接"); } if (TelephonyManager.EXTRA_STATE_IDLE.equals(phone_state)) { System.out.println("电话已挂断"); } return; } }
参考文章:
Android之TelephonyManager使用
Android 电话状态的监听(来电和去电)