简体   繁体   English

无论何时拨打电话,都要打开免提电话

[英]Turn on speakerphone whenever an outgoing call is made

My requirement is to turn on speakerphone whenever an outgoing call is initiated. 我的要求是每当拨打电话时打开免提电话。 I tried the following code, but it is not working. 我尝试了以下代码,但它无法正常工作。 In fact, speakerphone turns on when in the middle of a call, a second call comes ! 实际上,扬声器电话在通话过程中打开,第二个电话来了!

package in.co.allsolutions;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
//import android.view.View;
import android.widget.Toast;
import android.media.AudioManager;

public class MyTelephonyBroadcastReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {

            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setSpeakerphoneOn(true);
            Bundle extras = intent.getExtras();            
            if (extras != null) {

                  String state = extras.getString(TelephonyManager.EXTRA_STATE);
                  Log.i("AS", "Message Received. State = " + state + ", Mode = " + audioManager.getMode());
                  //audioManager.setMode(AudioManager.MODE_NORMAL);
                  //audioManager.setSpeakerphoneOn(true);
//                  if (state.equals("OFFHOOK"))
//                  {                  
                  //audioManager.setMode(AudioManager.MODE_CURRENT);
                  //audioManager.setSpeakerphoneOn(true);
                  //audioManager.setMode(AudioManager.MODE_IN_CALL);
                  //audioManager.setSpeakerphoneOn(true);
                  //audioManager.setMode(AudioManager.MODE_RINGTONE);
                  //audioManager.setSpeakerphoneOn(true);
                  if (audioManager.isSpeakerphoneOn()) {
                        Log.i("AS", "Speaker on - SUCCESS.");
                  } else {
                        Log.i("AS", "Speaker could not be turned on.");
                  }
//                  }
            } else {
                  Toast.makeText(context, "Message Received without any state", Toast.LENGTH_LONG).show();
            }
      }
}

Thanks. 谢谢。

You can set it through programmatically as below : 您可以通过以下编程方式进行设置:

AudioManager audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE); 
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);

But, keep in mind that don't forgot to set speaker off when stop the call: 但是,请记住,停止通话时不要忘记关闭扬声器:

audioManager.setSpeakerphoneOn(false);

And, Set permission in manifest: 并且,在清单中设置权限:

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

This code is working fine for me.hope it will be helpful for you. 这段代码对我来说很好。这对你有帮助。

A similar question was asked and answered here. 在这里提出并回答类似的问题

I think the answer may be in your project's AndroidManifest.xml. 我想答案可能在你的项目的AndroidManifest.xml中。 Try adding: 尝试添加:

uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"

to your manifest, which will allow your app to modify the device's audio settings. 到你的清单,这将允许你的应用程序修改设备的音频设置。
You will also need to change your audioManager mode to MODE_IN_CALL: 您还需要将audioManager模式更改为MODE_IN_CALL:

audioManager.setMode(AudioManager.MODE_IN_CALL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM