Could some Java guru kindly convert the following codes into B4A code for me, if possible, please? I would greatly appreciate it!
I'm looking for a reliable solution as a background service to intercept volume key press events with screen off(just anything indicating key_press is enough for my app, either Up or Down key).
I've tried both intent based solution and BroadcastReceiver approach, and neither of them meets my need.
I found the following Java code on StackOverflow website.
TIA
I'm looking for a reliable solution as a background service to intercept volume key press events with screen off(just anything indicating key_press is enough for my app, either Up or Down key).
I've tried both intent based solution and BroadcastReceiver approach, and neither of them meets my need.
I found the following Java code on StackOverflow website.
Java code for detecting volume key press with screen off:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.media.VolumeProviderCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
public class PlayerService extends Service {
private MediaSessionCompat mediaSession;
[USER=69643]@override[/USER]
public void onCreate() {
super.onCreate();
mediaSession = new MediaSessionCompat(this, "PlayerService");
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PLAYING, 0, 0) //you simulate a player which plays something.
.build());
//this will only work on Lollipop and up, see https://code.google.com/p/android/issues/detail?id=224134
VolumeProviderCompat myVolumeProvider =
new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE, /*max volume*/100, /*initial volume level*/50) {
[USER=69643]@override[/USER]
public void onAdjustVolume(int direction) {
/*
-1 -- volume down
1 -- volume up
0 -- volume button released
*/
}
};
mediaSession.setPlaybackToRemote(myVolumeProvider);
mediaSession.setActive(true);
}
[USER=69643]@override[/USER]
public IBinder onBind(Intent intent) {
return null;
}
[USER=69643]@override[/USER]
public void onDestroy() {
super.onDestroy();
mediaSession.release();
}
}
manifest entry:
<application ...>
...
<service android:name=".PlayerService"/>
</application>
code in Activity:
[USER=69643]@override[/USER]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
startService(new Intent(this, PlayerService.class));
}
TIA