android 拨打紧急号码,通话时开启免提功能实现


对应拨打电话只需要调用ACTION_CALL加对应电话号码即可如下:

 void callPhone(){  
            Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:110")); //不能调用呼叫  
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
            startActivity(intent);  
        }  

但是当你拨打110,119,120等紧急电话时,你会发现无法自动呼出,还需要用户进行确认操作,那么能不能实现直接进行呼叫?

答案是可以的,但是前提app必须用系统级别权限,及system/app/目录下方可。

首先确保应用具有如下权限:

   <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
   <uses-permission android:name="android.permission.CALL_PHONE" />  
   <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>  
   <uses-permission android:name="android.permission.CALL_PRIVILEGED" />  

同样调用intent来开启功能,不过内容有些变化

void callPhone(String num){  
        Intent intent = new Intent("android.intent.action.CALL_PRIVILEGED",Uri.parse("tel:"+num));  
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
        startActivity(intent);  
    }  

然后在源码下编译,放到/system/app/即可使用。


开启拨通电话后设置开启扬声器功能:

首先,获取音频管理器:

audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

设置电话监听:

private class TelListner extends PhoneStateListener{
		boolean comingPhone=false;
		
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			   switch (state) {
               case TelephonyManager.CALL_STATE_IDLE:/* 无任何状态 */
            	   Log.d(tag, "phone idle");
                  if(this.comingPhone){
                	  this.comingPhone=false;
                	  backMyApp();
                	  setSpeekModle(false);
                  }
                   break;
               case TelephonyManager.CALL_STATE_OFFHOOK:/* 接起电话 */
                	Log.d(tag, "phone answer");
                	this.comingPhone=true;
                	setSpeekModle(true);
                   break;
               case TelephonyManager.CALL_STATE_RINGING:/* 电话进来 */
            	   Log.d(tag, "phone coming");
                   this.comingPhone=true;
                   setSpeekModle(true);
                   break;
               }
		 }
	}

注册监听类:

TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
	      TelListner listener = new TelListner() ;
	      telManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

开启或关闭免提功能:

void setSpeekModle(boolean open){
		//audioManager.setMode(AudioManager.ROUTE_SPEAKER);
        currVolume = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
        audioManager.setMode(AudioManager.MODE_IN_CALL); 

        if(!audioManager.isSpeakerphoneOn()&&true==open) {
          audioManager.setSpeakerphoneOn(true);
          audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                 audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL),
                 AudioManager.STREAM_VOICE_CALL);
        }else if(audioManager.isSpeakerphoneOn()&&false==open){
                 audioManager.setSpeakerphoneOn(false);
                 audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,currVolume,
                            AudioManager.STREAM_VOICE_CALL);
        }
	}

添加权限:

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




你可能感兴趣的:(android电话免提,android拨打紧急电话)