android - How to get info of currently playing music using broadcastreceiver while the app isn't running? -
i receive broadcasts music players while app in background.
i found following code here:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); intentfilter if = new intentfilter(); if.addaction("com.android.music.metachanged"); if.addaction("com.android.music.playstatechanged"); if.addaction("com.android.music.playbackcomplete"); if.addaction("com.android.music.queuechanged"); registerreceiver(mreceiver, if); } private broadcastreceiver mreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { string action = intent.getaction(); string cmd = intent.getstringextra("command"); log.v("tag ", action + " / " + cmd); string artist = intent.getstringextra("artist"); string album = intent.getstringextra("album"); string track = intent.getstringextra("track"); log.v("tag", artist + ":" + album + ":" + track); toast.maketext(currentmusictrackinfoactivity.this, track, toast.length_short).show(); } };
and works, while activity running. tried make static broadcastreceiver:
<receiver android:name=".receiver.musicbroadcastreceiver" android:exported="false"> <intent-filter> <action android:name="com.android.music.metachanged" /> <action android:name="com.android.music.playstatechanged" /> <action android:name="com.android.music.playbackcomplete" /> <action android:name="com.android.music.queuechanged" /> </intent-filter> </receiver>
the class:
public class musicbroadcastreceiver extends broadcastreceiver { public musicbroadcastreceiver() { } @override public void onreceive(context context, intent intent) { string action = intent.getaction(); string cmd = intent.getstringextra("command"); log.v("tag ", action + " / " + cmd); string artist = intent.getstringextra("artist"); string album = intent.getstringextra("album"); string track = intent.getstringextra("track"); log.v("tag", artist + ":" + album + ":" + track); toast.maketext(context, track, toast.length_short).show(); } }
and doesn't work. started searching , found this question, claims code above should work. doesn't, onreceive not called when start playing music or skip track. imust doing wrong, can't figure out what.
any appreciated. thanks.
try set android:exported="true"
http://developer.android.com/guide/topics/manifest/receiver-element.html
android:exported
whether or not broadcast receiver can receive messages sources outside application — "true" if can, , "false" if not. if "false", messages broadcast receiver can receive sent components of same application or applications same user id.
Comments
Post a Comment