gpt4 book ai didi

android - 在后台服务中收听音量按钮?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:27 27 4
gpt4 key购买 nike

我知道如何在 Activity 中收听音量按钮。但是我可以在后台服务中这样做吗?如果是,该怎么做?

最佳答案

这是可能的。使用下面的代码(对于较新的 Android 版本,尤其是 Marshmallow,请参阅答案底部):

public class SettingsContentObserver extends ContentObserver {
int previousVolume;
Context context;

public SettingsContentObserver(Context c, Handler handler) {
super(handler);
context=c;

AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
previousVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
}

@Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}

@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);

AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);

int delta=previousVolume-currentVolume;

if(delta>0)
{
Logger.d("Ściszył!"); // volume decreased.
previousVolume=currentVolume;
}
else if(delta<0)
{
Logger.d("Zrobił głośniej!"); // volume increased.
previousVolume=currentVolume;
}
}
}

然后在您的服务 onCreate 中注册:

mSettingsContentObserver = new SettingsContentObserver(this,new Handler());
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver );

然后在onDestroy注销:

getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);

注意这个例子是根据媒体音量的变化来判断的,如果你想使用其他音量,改变它!

更新:

以上方法据说不适用于 Marshmallow,但自从引入 MediaSession 以来,现在有了更好的方法!因此,首先您必须将代码迁移到 MediaController/MediaSession 模式,然后使用此代码:

private VolumeProviderCompat myVolumeProvider = null;

myVolumeProvider = new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE, maxVolume, currentVolume) {
@Override
public void onAdjustVolume(int direction) {
// <0 volume down
// >0 volume up

}
};

mSession.setPlaybackToRemote(myVolumeProvider);

即使屏幕关闭,也能以某种方式检测到音量按钮按下(如果适用于您的平台,请确保注册正确的媒体按钮 Intent 接收器!)

更新 2 因为 GalDude 请求了一些关于获取媒体 MediaSession/MediaController 的更多信息。抱歉,但由于我不再使用 Java,它将使用 Kotlin:

lateinit var mediaSession: MediaSessionCompat // you have to initialize it in your onCreate method
val kontroler: MediaControllerCompat
get() = mediaSession.controller // in Java it's just getController() on mediaSession

// in your onCreate/start method:
mediaSession = MediaSessionCompat(this, "YourPlayerName", receiver, null)
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
mediaSession.isActive = true
if (ratingIsWorking) // note: rating crashes on some machines you have to check it!
mediaSession.setRatingType(RatingCompat.RATING_5_STARS)

mediaSession.setCallback(object : MediaSessionCompat.Callback() {
...
// here you have to implement what happens with your player when play/pause/stop/ffw etc. is requested - see exaples elsewhere
})

// onDestroy/exit method:
mediaSession.isActive = false
mediaSession.release()

关于android - 在后台服务中收听音量按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12685019/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com