6

我的应用程序中有一个在后端运行的服务,当我按下按钮音量调高时可以启动该服务,当我按下按钮音量调低时可以停止该服务。

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_RING);

}



@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_RING);

    int delta=previousVolume-currentVolume;



        if(delta > 0)
        {
            System.out.println("Decreased") ;
            Intent intent = new Intent(context, MyAlarmService.class);
            context.stopService(intent);
            previousVolume=currentVolume;
        }
        else if(delta < 0)
        {
              System.out.println("Increased");
              Intent intent = new Intent(context, MyAlarmService.class);
              context.startService(intent);
              previousVolume=currentVolume;
        }


}
}

我想在手机锁定时(屏幕关闭时)做同样的事情,当我在网上搜索时发现我必须使用广播接收器,我测试它但它不起作用。

public class YourBoardcastReceiver extends BroadcastReceiver {
    @Override
public void onReceive(Context context, Intent intent) {


    Log.e("get something", "i dont know what!!");

    String intentAction = intent.getAction();
    KeyEvent event = null;
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        event = (KeyEvent) intent
            .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    }

    if (event == null) {
        return;
    }

    int keycode = event.getKeyCode();
    int action = event.getAction();
    long eventtime = event.getEventTime();

    if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
        || keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
    if (action == KeyEvent.ACTION_DOWN) {

            Intent intent1 = new Intent(context, MyAlarmService.class);
            context.stopService(intent1);

        if (isOrderedBroadcast()) {
            abortBroadcast();
        }

        else if (action == KeyEvent.ACTION_UP) {

              Intent intent11 = new Intent(context, MyAlarmService.class);
              context.startService(intent11);

        }

            if (isOrderedBroadcast()) {
                abortBroadcast();


    }
}
}
}


}

在 android 清单中,我添加:

<receiver android:name="YourBoardcastReceiver">
            <intent-filter>
                    <action android:name="android.intent.action.SCREEN_ON" />
            </intent-filter>
    </receiver>

在方法 oncrete 的 mainActivity 中,我添加:

AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
ComponentName mRemoteControlResponder = new ComponentName(getPackageName(),
YourBoardcastReceiver.class.getName());

请问我可以给我一些帮助,思考。

4

2 回答 2

6

当手机处于锁定/睡眠模式时,我们无法收听按钮的变化,但是我们可以做一些技巧来收听音量按钮。

Github:https ://github.com/praslnx8/volume_Screen_Lock

这些应用程序在后台播放音量为 0 的媒体,然后 Reciever 将始终使用 MediaButtonIntent 收听密钥

于 2016-02-03T05:02:20.993 回答
0

尝试向广播接收器注册 Intent.ACTION_SCREEN_OFF 和 Intent.ACTION_SCREEN_ON

于 2015-08-20T14:11:14.453 回答