其實你是沒辦法控制其他app,只是把audio這個服務搶過來。具體原理還沒搞清楚。先提供代碼給你們,能用最好。
public void stopOtherAppVoice(final Context context){
this.mAudioManager = (AudioManager)context.getApplicationContext().getSystemService("audio");
if (!this.isHeadphonesPlugged(this.mAudioManager)) {
this._sensorManager = (SensorManager)context.getApplicationContext().getSystemService("sensor");
if (this._sensorManager != null) {
this._sensor = this._sensorManager.getDefaultSensor(8);
this._sensorManager.registerListener(this, this._sensor, 3);
}
}
this.muteAudioFocus(this.mAudioManager, true);
}
@TargetApi(8)
private void muteAudioFocus(AudioManager audioManager, boolean bMute) {
synchronized(this.mLock) {
if (audioManager != null) {
if (bMute) {
audioManager.requestAudioFocus(this.afChangeListener, 3, 2);
} else {
audioManager.abandonAudioFocus(this.afChangeListener);
this.afChangeListener = null;
}
}
}
}
關鍵代碼就這些,要完整代碼的,私信我。