How to detect key events in service and broadcast receiver - android

How can I get key events when a user presses various buttons in a service or broadcast receiver? I'm specifically interested in knowing when the user presses a volume button so that I can trigger something else in the background, like a voice recorder.
Unfortunately, my internet searches haven't produced any results.

Something like the following should work:
From official documentation:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"...>
//code, like activities, etc
<receiver android:name="com.example.test.VolumeBroadcast" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</application>
Example of a receiver:
public class VolumeBroadcast extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
//check the intent something like:
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
// Handle key press.
}
}
}
}
The way you register is like that:
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
// Start listening for button presses
am.registerMediaButtonEventReceiver(RemoteControlReceiver);
// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
Page below:
Audio Playback

Related

Support broadcast events from any Music player android

I'm developing an application which should do an action whenever a user starts or stops music player in android. I researched on it and found a way to do. But it only receives events from only default music player. Other streaming player like Rdio, last.fm were left behind.
The code i used
Manifest.xml
<receiver
android:name=".MediaStateReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="com.android.music.playstatechanged" />
</intent-filter>
</receiver>
MediaStateReceiver.java
public class MediaStateReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
LogUtil.print("broadcast event recieved " + intent.getAction());
if(intent.hasExtra("playing")) {
Intent mediaIntent = new Intent(context, MusicStateChangeService.class);
mediaIntent.putExtra("MUSIC_STATE", true);
mediaIntent.putExtra("playing",intent.getBooleanExtra("playing",false));
context.startService(mediaIntent);
}
}
}
Is there any way to receive broadcast events from any music player.?

How to launch particular application when Power Button is pressed twice in Android [duplicate]

i want to create such application in android which will be activated when i press sleep or power button twice , is it possible to do that , by running an application in background and listening events from power button ?
some times phone gets into sleep mode once it is idle , and to use any application user has to press
sleep button and then he has to enter certain password to activate the phone. But i want to make make it activate my application when a power button is clicked without any other intervention
You can try this trick .
Register a Broadcast Receiver which is initiated when powerbutton is clicked.
Now in OnReceive method of the Receiver do what you want.
For example:
in manifest file register a receiver:
<receiver android:name="com.test.check.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"></action>
<action android:name="android.intent.action.SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
</intent-filter>
</receiver>
&& in onReceive() method of the Receiver
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.v("##%#%#", "Power button is pressed.");
Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();
//perform what you want here
}
}
Now perform any operation in onReceive() method of the Receiver.
this is an key down event of power you can get some idea from this just try on this you'll get some idea...
i didnt try such an act but i found this in power manager.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// do what you want with the power button
return true;
}
return super.onKeyDown(keyCode, event);
}

Android - How to capture volume buttons events when the phone is in stand by

I need your help. I want to implement the volume buttons.
I want to know how can I capture volume button events when the phone is in stand-by?
Create a broadcast receiver for receiving your button events like this,
public class YourBoardcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
/* handle media button intent here by reading contents */
/* of EXTRA_KEY_EVENT to know which key was pressed */
}
}
}
and in manifest,
<receiver android:name="YourBoardcastReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>

activate an application when a power button is clicked

i want to create such application in android which will be activated when i press sleep or power button twice , is it possible to do that , by running an application in background and listening events from power button ?
some times phone gets into sleep mode once it is idle , and to use any application user has to press
sleep button and then he has to enter certain password to activate the phone. But i want to make make it activate my application when a power button is clicked without any other intervention
You can try this trick .
Register a Broadcast Receiver which is initiated when powerbutton is clicked.
Now in OnReceive method of the Receiver do what you want.
For example:
in manifest file register a receiver:
<receiver android:name="com.test.check.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"></action>
<action android:name="android.intent.action.SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
</intent-filter>
</receiver>
&& in onReceive() method of the Receiver
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.v("##%#%#", "Power button is pressed.");
Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();
//perform what you want here
}
}
Now perform any operation in onReceive() method of the Receiver.
this is an key down event of power you can get some idea from this just try on this you'll get some idea...
i didnt try such an act but i found this in power manager.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// do what you want with the power button
return true;
}
return super.onKeyDown(keyCode, event);
}

Capturing Intent.ACTION_MEDIA_BUTTON event

I'm developing a Android audio application. Now, I'm capturing
and playing audio with sucess and I want to add a new feature, the capture and playing through bluetooh headset.
I have been reading about that, and seems that I must manage the ACTION_MEDIA_BUTTON
event:
Java file:
....
public class audioBroadcastReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()))
{
Log.d("","#### WORKS" );
}
else
{
Log.d("","#### ????" );
}
....
xml file
....
<receiver android:name="audioBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON">
</action>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.BLUETOOTH" />
But nothing happens, so, somebody may give me a example or a idea for:
1º Know when the bluetooh has been connected.
2º Routed the audio packets through bluetooh headset.
Thanks!
You need the bluetoothAdapter method.
BluetoothAdapter blueT = BluetoothAdapter.getDefaulAdapter();
// create broadcast receiver
BroadcastReceiver blueReceiver = new BroadcastReceiver(){
// todo switch states to turn on or off or check is on
// check is on is something like this:
case (blueT.STATE_ON) : {
// do something with it
}
if(blueT.isEnabled()){
do something
}

Categories

Resources