我看到了这个话题
Listen to volume buttons in background service?
并且知道在后台服务中持续使用音量键的方法是检测系统音量(或音乐......)并查看它是否减少或增加我们可以检测按钮
我看到一个应用程序在锁定模式或屏幕关闭时一起检测按下音量增大/减小按钮,并且它正在使用AccessibilityService。怎么可能?
https://play.google.com/store/apps/details?id=in.blogspot.anselmbros.torchie&hl=en
答案 0 :(得分:1)
您可以在服务中创建BroadcastReceiver。
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
final BroadcastReceiver vReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//your code here
}
};
registerReceiver(vReceiver, new IntentFilter("android.media.VOLUME_CHANGED_ACTION"));
}
}
或在背景中播放0卷的媒体,然后接收者将使用MediaButtonIntent监听该密钥,如this GitHub sample
所示