gpt4 book ai didi

android - 每当拨出电话时打开免提电话

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:17 27 4
gpt4 key购买 nike

我的要求是在拨出电话时打开扬声器。我尝试了以下代码,但它不起作用。事实上,当通话进行到第二个来电时,扬声器会打开!

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

谢谢。

最佳答案

您可以通过编程方式设置它,如下所示:

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

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

audioManager.setSpeakerphoneOn(false);

并且,在 list 中设置权限:

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

此代码对我来说工作正常。希望对您有所帮助。

关于android - 每当拨出电话时打开免提电话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8617381/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com