0

my problem is that when I try to register a receiver to the music player

 IntentFilter iF = new IntentFilter();
 iF.addAction("com.android.music.metachanged");
 iF.addAction("com.android.music.playstatechanged");
 registerReceiver(mReceiver, iF);

this receiver is launched when any player runs. I want to get notified in the case of the default music player changes only. How can I get the unique action of the default player?

iKilany
  • 71
  • 10
  • What exactly are you trying to achieve I don't understand. What does your app do? – Squonk Apr 12 '15 at 11:34
  • I am developing a controller for the device music player, this controller is linked to service not activity (floating window) so I need to track and control the music player , not every music player.this recevier distracts me. – iKilany Apr 12 '15 at 11:50

1 Answers1

0

This happens because you are listening on the MusicPlayer action not the application package name. All the music players lunch the same action com.android.music.playstatechanged

You need to filter this action with the package name of the application. I have not tried that by myself with the music player, but give it a try. Add this check line in your receiver. You can replace this package name with any other package for any application

if (intent.getAction().substring(0, action.lastIndexOf(".")).equals("com.google.android.music")) {

}
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • @iKilany try it with another music player on your device by changing the current package name with its package name, and debug to make sure if it is working – Sami Eltamawy Apr 12 '15 at 11:53
  • i think i did this, may be i can't understand the last comment properly – iKilany Apr 12 '15 at 12:53
  • @iKilany Test it by replacing the current 'com.google.android.music' with any other package name for any installed app and then try again to see if you can detect the package or not. – Sami Eltamawy Apr 12 '15 at 12:54
  • using intent.getDataString() returns null, i detect the package name using , intent.getAction().substring(0, action.lastIndexOf(".")) , and I tried to detect "com.google.android.music", and its is not found – iKilany Apr 12 '15 at 13:20
  • @iKilany I'm using Samsung device and the default music player package name is `com.samsung.musicplus` which means that you have to check your default from your device from the application manager and try to detect it – Sami Eltamawy Apr 12 '15 at 13:24
  • @iKilany don't try to detect `com.google.android.music`, try to know what exactly the package name of the app you want to detect then write it down – Sami Eltamawy Apr 12 '15 at 13:26
  • when using the default i get "com.android.music" , when using another one the receiver is called twice one from "com.android.music" the other from the apps package name "com.andrew.apollo", thats why i can't detect the default one this way. I'm using samsung device also . – iKilany Apr 12 '15 at 13:31
  • @iKilany I think now you know your issue and close to the solution. Remember if you found my answer or help useful vote it up or accept it as answer – Sami Eltamawy Apr 12 '15 at 13:33
  • @iKilany Here when someone waste some of his time with someone else trying to help him, the least thing to thank him is to vote up or accept his answer accodring to help. I hope you got it – Sami Eltamawy Apr 12 '15 at 14:12