0

I implemented an app which has a button speaker, which is manually set "on" by myAudioManager.setSpeakerphoneOn(true) and "off" vice-versa. But the bug is when you close the application while the speaker is on, it forgets the speaker was on. The next time, the speaker remains on and the call is by default in speaker mode. I don't want to set speaker off when app is in background, only when the app gets killed it has to be set off. onDestroy() works good, but it wont gets called all the time according to the lifecycle.

lijo050
  • 233
  • 4
  • 14
  • Maybe set speaker off in your `onPause()` or `onDestroy()` method, depends on which you prefer. – Strider Jun 22 '15 at 07:19
  • I don't want to set speaker off when app is in background, only when the app gets killed it has to be set off. onDestroy() works good, but it wont gets called all the time according to the lifecycle – lijo050 Jun 22 '15 at 07:26
  • Well... in that case I suggest you should use a service. – Strider Jun 22 '15 at 07:36
  • @Strider any code snippet will be apreciated. – lijo050 Jun 22 '15 at 07:48

2 Answers2

1

Use a service like:

public class FirstService extends Service {

    private AudioManager audioManager;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        audioManager.setSpeakerphoneOn(false);  //Turn of speaker
    }
}

public void onDestroy () Added in API level 1

Called by the system to notify a Service that it is no longer used and is being removed. The service should clean up any resources it holds (threads, registered receivers, etc) at this point. Upon return, there will be no more calls in to this Service object and it is effectively dead. Do not call this method directly. More info here

Add service to Manifest:

<service android:name=".FirstService" ></service>

And start the service in your activity's onCreate() method with:

startService(new Intent(this, FirstService.class));

Hope this helps you out.

Strider
  • 4,452
  • 3
  • 24
  • 35
0

You need to manage audio manager for android lifecycle's evry method.

Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39
  • any clue how to do that – lijo050 Jun 22 '15 at 07:20
  • You have to stop speaker and release objects in onDestroy(). – Samir Bhatt Jun 22 '15 at 07:24
  • but onDestroy() wont gets called all the time according to the lifecycle. Sometimes through onStop() the app gets closed and i don't want to set speaker off when the app is in background. – lijo050 Jun 22 '15 at 07:29
  • Then you have to manage it in onStop(). When your app goes in background, onPause() will call. So, You need to manage it in onStop() and onPause(). Also, have to manage in onResume() and onRestart(). – Samir Bhatt Jun 22 '15 at 07:35
  • 1
    @SamirBhatt `onStop()` also get's called when leaving the app. See [this](http://developer.android.com/reference/android/app/Activity.html#onStop%28%29) link – Strider Jun 22 '15 at 07:40
  • @SamirBhatt He doesn't want his speaker to turn off when his app goes to the background, see second comment on his own post ;) – Strider Jun 22 '15 at 07:43
  • @Samir i didn't get you. I think when the app goes in background onStop() is called and onPause() when app is partially visible. – lijo050 Jun 22 '15 at 07:45
  • ok. got it. as i have mention in first comment, you can manage it in onDestroy().You can also manage if you want to play music through speaker or any other resources in onstart() or onRestart(). – Samir Bhatt Jun 22 '15 at 07:50