当应用程序不是 运行 时,如何使用 broadcastreceiver 获取当前正在播放的音乐的信息?
How to get info of currently playing music using broadcastreceiver while the app isn't running?
我想在应用程序处于后台时接收来自音乐播放器的广播。
我找到了下面的代码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();
}
};
它有效,但仅当 activity 为 运行 时。所以我尝试制作一个静态广播接收器:
<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>
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();
}
}
而且它不起作用。
我开始搜索并找到 this question,它声称上面的代码应该可以工作。但事实并非如此,当我开始播放音乐或跳过曲目时,不会调用 onReceive。我一定是做错了什么,但我不知道是什么。
如有任何帮助,我们将不胜感激。谢谢。
尝试设置android:exported="true"
http://developer.android.com/guide/topics/manifest/receiver-element.html
android:exported
Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.
有关于 "com.android.music.playstatechanged" 用法的信息
如前所述,此方法仅适用于标准播放器。
API 中未描述此类通知。可能它已被删除。
我想在应用程序处于后台时接收来自音乐播放器的广播。
我找到了下面的代码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();
}
};
它有效,但仅当 activity 为 运行 时。所以我尝试制作一个静态广播接收器:
<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>
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();
}
}
而且它不起作用。 我开始搜索并找到 this question,它声称上面的代码应该可以工作。但事实并非如此,当我开始播放音乐或跳过曲目时,不会调用 onReceive。我一定是做错了什么,但我不知道是什么。
如有任何帮助,我们将不胜感激。谢谢。
尝试设置android:exported="true"
http://developer.android.com/guide/topics/manifest/receiver-element.html
android:exported
Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.
有关于 "com.android.music.playstatechanged" 用法的信息 如前所述,此方法仅适用于标准播放器。
API 中未描述此类通知。可能它已被删除。