0
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setData(Uri.parse("tel:" + sharedPreferencesUtils.getStringFromSharedPreferences(KEY_SP_MOBILE_NUMBER, context.getApplicationContext())));
context.getApplicationContext().startActivity(callIntent);

Thread thread = new Thread() {
    @Override
    public void run() {
        try {
            while(true) {
                sleep(300);//1000
                audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setMode(AudioManager.MODE_IN_CALL);
                if (!audioManager.isSpeakerphoneOn()) {
                    audioManager.setSpeakerphoneOn(true);
                }
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};

thread.start();
Cœur
  • 37,241
  • 25
  • 195
  • 267
Aniket
  • 1
  • 1

2 Answers2

0

I put bottom code run inside a thread. It worked well. This is my code. Hope it can help someone

        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    while(true) {
                        sleep(1000);
                        audioManager.setMode(AudioManager.MODE_IN_CALL);
                        if (!audioManager.isSpeakerphoneOn())
                            audioManager.setSpeakerphoneOn(true); //or false
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        thread.start();

Be sure to include the MODIFY_AUDIO_SETTINGS permission in your manifest.

  • You just copy-paste it from here: https://stackoverflow.com/a/40041975/6721828 You should add the link in the comment for already given answer instead – Edhar Khimich Jul 01 '19 at 05:18
  • Hii , i got a solution for this final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager.setMode(AudioManager.MODE_IN_CALL); audioManager.setSpeakerphoneOn(true); } }, 1000); – Aniket Jul 10 '19 at 04:45
  • I have put this code inside handler then call is genrated from app is going on speaker phone and other call which we dial from defalut call app is going on normal mode and receiving call also goes in to the normal mode this is working properly. – Aniket Jul 10 '19 at 04:48
0

Try this one

audiomanager.setSpeakerphoneOn(false)

For it you should add the MODIFY_AUDIO_SETTINGS permission in manifest.

Edhar Khimich
  • 1,468
  • 1
  • 17
  • 20