如何在应用程序被 "Recent App drawer" 杀死时关闭扬声器 phone
How to set speaker phone off when the app is killed by the "Recent App drawer"
我实现了一个带有按钮扬声器的应用程序,它由 myAudioManager.setSpeakerphoneOn(true) 手动设置 "on",反之亦然 "off"。但是这个错误是当你在扬声器打开时关闭应用程序时,它会忘记扬声器是打开的。下一次,扬声器保持打开状态,通话默认处于扬声器模式。我不想在应用程序处于后台时关闭扬声器,只有当应用程序被杀死时才必须关闭它。 onDestroy() 效果很好,但它不会根据生命周期一直被调用。
您需要为 android 生命周期的每个方法管理音频管理器。
使用如下服务:
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
向清单添加服务:
<service android:name=".FirstService" ></service>
并在您的 activity 的 onCreate()
方法中启动服务:
startService(new Intent(this, FirstService.class));
希望这对您有所帮助。
我实现了一个带有按钮扬声器的应用程序,它由 myAudioManager.setSpeakerphoneOn(true) 手动设置 "on",反之亦然 "off"。但是这个错误是当你在扬声器打开时关闭应用程序时,它会忘记扬声器是打开的。下一次,扬声器保持打开状态,通话默认处于扬声器模式。我不想在应用程序处于后台时关闭扬声器,只有当应用程序被杀死时才必须关闭它。 onDestroy() 效果很好,但它不会根据生命周期一直被调用。
您需要为 android 生命周期的每个方法管理音频管理器。
使用如下服务:
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
向清单添加服务:
<service android:name=".FirstService" ></service>
并在您的 activity 的 onCreate()
方法中启动服务:
startService(new Intent(this, FirstService.class));
希望这对您有所帮助。