Search code examples
javaandroidandroid-intentandroid-activityandroid-developer-api

How to click song list (Now Playing activity) to playing without (Music Preview) startActivity


How to click and send (Intent + putExtras + startActivity) in main activity (music preview activity) and playing song without showing it. Song list stays, but clicking list song --> refresh music preview activity. I want stay this activity (song list) but music priview activity get intent and refreshing and playing new song.

http://www.pixentral.com/pics/1MpvIjTXByBNfFasIZ0UFI2SbkS11.jpg http://www.pixentral.com/pics/1MpvIjTXByBNfFasIZ0UFI2SbkS11.jpg

song list :

public void onClick(View v) {

            Intent intent = new Intent(mContext, MusicPreview.class);
            intent.putExtra("pos", position).putExtra("names", SongsName).putExtra("songlist", SongsUri);
            mContext.startActivity(intent);

        }

Solution

  • You could use a BroadcastReceiver for this.

    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                //Get all your music data from the intent
                String position = intent.getStringExtra("pos");
                ...
            }
     };
    

    Register this BroadcastReciever with an Intent in your NowPlayingActivity's onCreate() like this,

    registerReceiver(mHandleMessageReceiver, new IntentFilter("songchange"));
    

    And don't forget to unregister the receiver onDestroy().

    unregisterReceiver(mHandleMessageReceiver);
    

    And then in your PlaylistActivity on List item click do this,

    Intent intent = new Intent("songchange");
    intent.putExtra("pos", position);
    ...//pass all the needed data
    sendBroadcast(intent);