Didi78
Didi78

Reputation: 247

Start call with speaker via intent

I have a service that start a call when something happened, that works prefect but my question is how can I start it with speaker on?(and remove the speaker after 2 min?)


according to your all anwsers,

this code should work -

                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + num));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_FROM_BACKGROUND);// (i'm starting the call from service..)
                startActivity(intent); //the call start here, work perfect
                AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                audioManager.setMode(AudioManager.MODE_IN_CALL);
                audioManager.setSpeakerphoneOn(true);

well, It doesnt work.. why?

Upvotes: 2

Views: 1968

Answers (2)

Rishabh Singh Bisht
Rishabh Singh Bisht

Reputation: 150

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

Use this to turn speaker on once call is picked up.

Premission

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

Register PhoneStateListener to know when call is picked up. urn speaker on when call state is TelephonyManager.CALL_STATE_OFFHOOK

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(false);
audioManager.setMode(AudioManager.MODE_NORMAL);

Upvotes: 4

xenteros
xenteros

Reputation: 15842

Use an AudioManager to turn on the speakers and a CallStateListener for checking if the call is over. If is not, check if it lasts two minutes.

Upvotes: 0

Related Questions