我有广播接收器应该获得当前正在播放的歌曲信息。如果我在活动中注册它,它工作正常,但如果我在清单文件中注册它就不起作用。
我如何在AndroidManifest中注册接收器:
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".SongReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.music.playstatechanged" />
<action android:name="com.android.music.metachanged" />
</intent-filter>
</receiver>
</application>
我的BroadcastReceiver:
class SongReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
val cmd = intent.getStringExtra("command")
Log.d("mIntentReceiver", "$action / $cmd")
val artist = intent.getStringExtra("artist")
val track = intent.getStringExtra("track")
Toast.makeText(context, "$artist - $track", Toast.LENGTH_LONG).show()
}
}
我尝试使用像这样的完整软件包名称,但它没有帮助:
<receiver
android:name="com.fullpackage.SongReceiver"
...
</receiver>