I am trying to have a background service using which I can play or pause media playback on another app. I believe the basic idea is to send KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE key to the other app. I tried to use SendInput library but the problem is it sends keys to the system, which means it will get executed in the app that was the last one to play media. I couldn't find a way to link key input to a specific package name.
This java code seems to do it. Since it uses intents, there must be a way to set the component name to a specific app. Could you please translate to B4A?
This java code seems to do it. Since it uses intents, there must be a way to set the component name to a specific app. Could you please translate to B4A?
Java:
private static void sendMediaButton(Context context, int keyCode)
{
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intent, null);
keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intent, null);
}
sendMediaButton(getApplicationContext(), KeyEvent.KEYCODE_MEDIA_PAUSE);