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

时间:2011-12-23 14:53:12

标签: android speakerphone

我的要求是在启动拨出电话时打开免提电话。我尝试了以下代码,但它无法正常工作。事实上,扬声器电话在通话过程中打开,第二个电话来了!

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();
            }
      }
}

感谢。

2 个答案:

答案 0 :(得分:9)

您可以通过以下编程方式进行设置:

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

但是,请记住,在停止通话时不要忘记关闭扬声器:

audioManager.setSpeakerphoneOn(false);

并且,在清单中设置权限:

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

这段代码对我来说很好。这对你有帮助。

答案 1 :(得分:5)

提出了类似问题并回答了here.

我认为答案可能在您项目的AndroidManifest.xml中。尝试添加:

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

到你的清单,这将允许你的应用修改设备的音频设置 您还需要将audioManager模式更改为MODE_IN_CALL:

audioManager.setMode(AudioManager.MODE_IN_CALL)