Support broadcast events from any Music player android - 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.?

Related

How to detect key events in service and broadcast receiver

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

Priority two apps use bootcompletedReceiver on android

recently I use BOOT_COMPLETED 2 app (A app, and B app)
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
A app is activity and B app is service app
when my device boot,
first A app launch and B app launch
so, show B app screen.
I want
first B app launch and A app launch showing A app screen
perhaps, Can I give BOOT_COMPLETED Priority is possible?
finally, I want when I boot my device, show A app screen
Thanks!
add
I try
B app(service)
public class BootCompletedReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) {
Intent i = new Intent("A app package name.BOOT_COMPLETED");
context.sendBroadcast(i);
}
}
}
<receiver android:name=".BootCompletedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
A app(activity)
public class BootSendReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context,Intent intent) {
if( intent.getAction().equals("B app packagename.BOOT_COMPLETED"));
Intent i = new Intent (context, MainActivity.class);
context.startActivity(i);
}
}
<receiver android:name=".BootSendReceiver">
<intent-filter>
<action android:name="blackeyeonandroid.iosystem.co.kr.simpleserviceexample.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
and I try boot .. but showing B app screen
I think you can not control that..
Instead, you can make APP1 start the APP2. This way, only APP1 receives the BOOT_COMPLETE message. Then, APP1 is responsible to send a new intent to start APP2:
Maybe, you can do as follows (note that APP2 does not receive android's default BOOT_COMPLETED message):
APP1
Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".AppToStartFirstBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Receiver
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent("com.example.mytestapp.BOOT_COMPLETED");
context.sendBroadcast(i);
}
}
APP2
Manifest:
<receiver android:name=".AppToStartLaterBroadcastReceiver">
<intent-filter>
<action android:name="com.example.mytestapp.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Receiver
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.example.mytestapp.BOOT_COMPLETED")) {
// Do what you want in secundary APP
}
}
Note
This is an suggestion and you should adjust to your case. Since I don't have more details about your code, you may need to modify it to your case.. But you can use the idea.

Restart android app on incoming call

I am making an app in which I want to start a service as soon as there is an incoming call. What are the various ways to do this in android?
I know broadcast receiver is one way, but I couldn't find any broadcast intent for incoming phone call.
Use action PHONE_STATE to detect incoming calls..
add this to manifest
<receiver android:name="com.example.YourReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
And your receiver
public class YourReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
// This code will execute when the phone has an incoming call
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is disconnected
}
}
}

Android start/stop service if USB device is plugged in/removed without Activity

I am trying to start a Service on Android as soon as a certain USB device is plugged in. I have already found this example, which launches an (invisible) activity with an intent-filter, which on the other hand launches the service.
However, this seems to me a little bit hackery and I am trying to do the same with a BroadcastReceiver with the following code:
public class UsbMessageReceiver extends BroadcastReceiver {
public UsbMessageReceiver() {
Log.d("UsbMessageReceiver", "Constructor");
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.d("UsbMessageReceiver", "onReceive");
throw new UnsupportedOperationException("Not yet implemented");
}
}
And the manifest:
<receiver
android:name=".UsbMessageReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
</receiver>
Edit: If forgot to mention that this approach does not work. I don't get the log message and the Error is not thrown. Is this possible without invoking an Activity?
Using bellow code launch the service in on recieve method
context.startService(new Intent(context.getApplicationContext(), MyService.class));

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>

Categories

Resources