0

I am using a BroadcastReceiver.

But it didn't work. When I had started playing music player and opened an application, Toast didn't show.

In debug all variables are null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.status);
    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");
    iF.addAction("com.miui.player.metachanged");
    iF.addAction("com.miui.player.playstatechanged");
    iF.addAction("com.miui.player.playbackcomplete");
    iF.addAction("com.miui.player.

    registerReceiver(mReceiver, iF);

}

public BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String cmd = intent.getStringExtra("command");
        Log.v("tagtag ", action + " / " + cmd);
        String artist = intent.getStringExtra("artist");
        String album = intent.getStringExtra("album");
        String track = intent.getStringExtra("track");
        Log.v("tagtag", artist + ":" + album + ":" + track);
        Toast.makeText(MainActivity.this, track, Toast.LENGTH_SHORT).show();
    }
};
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
OvconaG
  • 1
  • 2
  • There is no requirement for any music player to send a system broadcast about what it is playing. – CommonsWare Dec 21 '18 at 17:09
  • @CommonsWare Like this https://stackoverflow.com/questions/10510292/track-info-of-currently-playing-music – OvconaG Dec 21 '18 at 17:11
  • Also, most implicit broadcasts are banned on Android 8.0+. You would need to contact the developers of music player apps, see which of those apps have some sort of API or SDK, and integrate the ones that you choose. – CommonsWare Dec 21 '18 at 17:17

0 Answers0