jeudi 13 août 2015

How to get app's metadata from another app?

I am trying to add a feature to my app which receives broadcast data from Spotify and does something with that data. However, I am not sure how to proceed. I've been able to piece together the following code with the help of previous questions, however those questions do not go into detail on how to actually initiate the reception of data. I have a BroadCast Receiver:

<receiver
        android:name=".Receiver"
        android:enabled="true"
        android:exported="true">

        <intent-filter>
            <action android:name="com.spotify.music.playbackstatechanged"/>
            <action android:name="com.spotify.music.metadatachanged"/>
            <action android:name="com.spotify.music.queuechanged"/>
        </intent-filter>

    </receiver>

I have a BroadCast Receiver class (all set up):

public class Receiver extends BroadcastReceiver {

static final class BroadcastTypes {
    static final String SPOTIFY_PACKAGE = "com.spotify.music";
    static final String PLAYBACK_STATE_CHANGED = SPOTIFY_PACKAGE + ".playbackstatechanged";
    static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged";
}


@Override
public void onReceive(Context context, Intent intent) {

    long timeSentInMs = intent.getLongExtra("timeSent", 0L);
    String action = intent.getAction();

    if (action.equals(BroadcastTypes.METADATA_CHANGED)) {

        String trackId = intent.getStringExtra("id");
        String artistName = intent.getStringExtra("artist");
        String albumName = intent.getStringExtra("album");
        String trackName = intent.getStringExtra("track");
        int trackLengthInSec = intent.getIntExtra("length", 0);

        //send data to main class?

    } else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) {

        boolean playing = intent.getBooleanExtra("playing", false);
        int positionInMs = intent.getIntExtra("playbackPosition", 0);

        //send data to main class?
    }

  }
}

and I have a main Activity class. So now I am not sure how I can actually start the acquisition of data from Spotify in my main activity, or how to then send that data (from the receiver) back to my main activity for later use.

Thanks for any advice



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire