通知到达时音频管理器 onAudioFocusChange() 未触发
Audio Manager onAudioFocusChange() not firing when notification arrives
我想在通知声音出现之前执行一些操作。经过一番谷歌搜索后,我发现 OnAudioFocusChangeListener 是可行的方法。我制作了我的服务实现 AudioManager.OnAudioFocusChangeListener 但从未调用过 onAudioFocusChange() 方法。
public void onAudioFocusChange(int focusChange) {
Log.d(LOG_TAG, "Called!");
if(focusChange == AudioManager.AUDIOFOCUS_GAIN){
// Do STuff
}
}
有人可以帮助我吗,任何建议都将不胜感激!!
AudioManager.OnAudioFocusChangeListener
必须注册到您的 AudioManager
才能被正确调用。
//get the audiomanager
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
//create the listener
audioFocusListener=new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
// do your job here
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
}
}
};
//register the listener
audioManager.requestAudioFocus(audioFocusListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
//later unregister it when you do not need it anymore
AppContext.audioManager.abandonAudioFocus(audioFocusListener);
您可以使用例如Youtube 应用程序,它将触发此侦听器。
编辑
NotificationListenerService
AndroidManifest.xml
<service
android:name=".NotificationListenerService"
android:label="@string/app_name"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
NotificationListenerService.java:
public class NotificationListenerService extends android.service.notification.NotificationListenerService {
private static final String TAG = "NotificationListenerService";
public NotificationListenerService(){}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// do your job
}
}
要启用侦听器,您可以转到:
Settings > Sound & Notification > Notification Access > App > Enable
或从您的应用中调用:
Intent i=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(i);
我想在通知声音出现之前执行一些操作。经过一番谷歌搜索后,我发现 OnAudioFocusChangeListener 是可行的方法。我制作了我的服务实现 AudioManager.OnAudioFocusChangeListener 但从未调用过 onAudioFocusChange() 方法。
public void onAudioFocusChange(int focusChange) {
Log.d(LOG_TAG, "Called!");
if(focusChange == AudioManager.AUDIOFOCUS_GAIN){
// Do STuff
}
}
有人可以帮助我吗,任何建议都将不胜感激!!
AudioManager.OnAudioFocusChangeListener
必须注册到您的 AudioManager
才能被正确调用。
//get the audiomanager
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
//create the listener
audioFocusListener=new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
// do your job here
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
}
}
};
//register the listener
audioManager.requestAudioFocus(audioFocusListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
//later unregister it when you do not need it anymore
AppContext.audioManager.abandonAudioFocus(audioFocusListener);
您可以使用例如Youtube 应用程序,它将触发此侦听器。
编辑
NotificationListenerService
AndroidManifest.xml
<service
android:name=".NotificationListenerService"
android:label="@string/app_name"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
NotificationListenerService.java:
public class NotificationListenerService extends android.service.notification.NotificationListenerService {
private static final String TAG = "NotificationListenerService";
public NotificationListenerService(){}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// do your job
}
}
要启用侦听器,您可以转到:
Settings > Sound & Notification > Notification Access > App > Enable
或从您的应用中调用:
Intent i=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(i);