J
JonasSchmietzer
Neues Mitglied
- 1
Hallo zusammen,
ich möchte mit einen BroadcastReceiver die Information von der Spotify auslesen (Track,Album und Künstler-informationen).
Nur habe ich das Problem,dass keine Werte an die HomeActivity übergegeben werden.
Ich habe die packagename und eine Redirect URI bei Spotify erstellt,somit war auch die Anmeldung über meine App auch möglich.
Nur habe ich das Gefühl, dass der BroadcastReceiver nicht aktiv wird
MainActivity
(BroadcastSpotify) BroadcastReceiver
MyService.java
HomeActivity.java
Manifest.xml
Gruß,
Jonas
ich möchte mit einen BroadcastReceiver die Information von der Spotify auslesen (Track,Album und Künstler-informationen).
Nur habe ich das Problem,dass keine Werte an die HomeActivity übergegeben werden.
Ich habe die packagename und eine Redirect URI bei Spotify erstellt,somit war auch die Anmeldung über meine App auch möglich.
Nur habe ich das Gefühl, dass der BroadcastReceiver nicht aktiv wird

MainActivity
Java:
private static final int REQUEST_CODE = 1337;
private static final String CLIENT_ID = "";
private static final String REDIRECT_URI = "";
private static final String SCOPES = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
authenticateSpotify();
}
private void authenticateSpotify() {
AuthenticationRequest.Builder builder = new AuthenticationRequest.Builder(CLIENT_ID, AuthenticationResponse.Type.TOKEN, REDIRECT_URI);
builder.setScopes(new String[]{SCOPES});
AuthenticationRequest request = builder.build();
AuthenticationClient.openLoginActivity(this, REQUEST_CODE, request);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
// Check if result comes from the correct activity
if (requestCode == REQUEST_CODE) {
AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
switch (response.getType()) {
// Response was successful and contains auth token
case TOKEN:
// Handle successful response
Toast.makeText(getApplicationContext(), "the sign up to spotify was sucessful", Toast.LENGTH_SHORT).show();
Intent SwitchToHome = new Intent(MainActivity.this, HomeActivity.class);
startActivity(SwitchToHome);
finish();
break;
// Auth flow returned an error
case ERROR:
Toast.makeText(getApplicationContext(), "the sign up to spotify was not sucessful", Toast.LENGTH_SHORT).show();
// Handle error response
break;
// Most likely auth flow was cancelled
default:
Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
// Handle other cases
}
}
}
}
(BroadcastSpotify) BroadcastReceiver
Java:
public class BroadcastSpotify 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 QUEUE_CHANGED = SPOTIFY_PACKAGE + ".queuechanged";
static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged";
}
@Override
public void onReceive(Context context, Intent intent) {
// This is sent with all broadcasts, regardless of type. The value is taken from
// System.currentTimeMillis(), which you can compare to in order to determine how
// old the event is.
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);
Intent intentService = new Intent(context, MyService.class);
// add infos for the service which file to download and where to store
intentService.putExtra("trackId", trackId);
intentService.putExtra("artistName", artistName);
intentService.putExtra("albumName", albumName);
intentService.putExtra("trackName", trackName);
intentService.putExtra("trackLengthInSec", trackLengthInSec);
context.startActivity(intentService);
// Do something with extracted information...
} else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) {
boolean playing = intent.getBooleanExtra("playing", false);
int positionInMs = intent.getIntExtra("playbackPosition", 0);
// Do something with extracted information
} else if (action.equals(BroadcastTypes.QUEUE_CHANGED)) {
}
}
}
MyService.java
Java:
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
//@Override
public void onReceive(Intent intent) {
Log.i("SERVICE-Spotify", "Spotify");
String trackId = intent.getStringExtra("trackId");
String artistName = intent.getStringExtra("artistName");
String albumName = intent.getStringExtra("albumName");
String trackName = intent.getStringExtra("trackName");
String trackLengthInSec = intent.getStringExtra("trackLengthInSec");
Intent myIntent = new Intent(MyService.this, HomeActivity.class);
myIntent.putExtra("TrackName",trackName);
myIntent.putExtra("AlbumName",albumName);
startActivity(myIntent);
}
HomeActivity.java
Java:
Intent intent = getIntent();
String UserId=intent.getStringExtra("TrackName");
String UserName=intent.getStringExtra("AlbumName");
Toast.makeText(getApplicationContext(),UserId,Toast.LENGTH_SHORT).show();
Manifest.xml
Java:
<receiver
android:name="de.janoroid.spotifybroadcast.BroadcastSpotify"
android:enabled="true"
android:process=":remote"
android:exported="true">
<intent-filter>
<action android:name ="android.hardware.usb.action.USB_STATE" />
<!-- Spotify -->
<action android:name="com.spotify.music.playbackstatechanged" />
<action android:name="com.spotify.music.metadatachanged" />
<action android:name="com.spotify.music.queuechanged" />
<action android:name="com.spotify.music.active"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service
android:name="de.janoroid.spotifybroadcast.MyService"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE" />
</intent-filter>
</service>
</application>
</manifest>
Gruß,
Jonas