本文主要是介绍android 电话状态简介,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转自:http://blog.csdn.net/java2009cgh/article/details/7166009
打电话和来电话时处理 媒体音量的问题。现将逻辑说明如下:
1.来电话
监听类 PhoneStateListener
第一种状态:响铃:TelephonyManager.CALL_STATE_RINGING
第二种状态:接起电话: TelephonyManager.CALL_STATE_OFFHOOK
第三种状态:挂电话:TelephonyManager.CALL_STATE_IDLE
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(DBG) Log.v(TAG,"onCallStateChanged()");
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// 当有电话进来时
if (state == TelephonyManager.CALL_STATE_RINGING) {
mbPhoneCall = true;
curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);// 获取当前音量值
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);//设置当前音量值为0
} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
// 当有电话在等时
if(!mbPhoneCall) {
mbPhoneCall = true;
curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);// 获取当前音量值
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);//设置当前音量值为0
}
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
// 当挂断电话时
if(mbPhoneCall) {
mbPhoneCall = false;
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, curVolume, 0); //还原音量值
}
}
}
};
2 打电话
HTC的手机开发过程中,在打电话的时候,没有发送该广播,只触发了电话监听类。状态为 TelephonyManager.CALL_STATE_OFFHOOK
第一种状态:打电话 监听类会触发 state = TelephonyManager.CALL_STATE_OFFHOOK,但是广播不一定能收到,HTC的手机就不会。
第二种状态:对方接通电话,state = TelephonyManager.CALL_STATE_OFFHOOK
第三种状态:挂电话,state = TelephonyManager.CALL_STATE_IDLE
// 添加电话监听类
IntentFilter filterOutCall = new IntentFilter();
filterOutCall.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
registerReceiver(mPhoneOutCallListener, filterOutCall);
广播接收 private BroadcastReceiver mPhoneOutCallListener = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mbPhoneCall = true;
}
};
将这些记录下来,总要是说明:打电话的接收广播,在一些手机中,是接收不到的,比如HTC的,所以打电话的处理逻辑必须在电话监听类中state == TelephonyManager.CALL_STATE_OFFHOOK进行处理。
权限: <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 电话监听类的权限<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> 打电话的监听权限
这篇关于android 电话状态简介的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!