Intent überträgt die Variablen nicht an MainActivity.java

  • Antworten:5
  • OffenNicht stickyBentwortet
  • Forum-Beiträge: 63

18.01.2020, 02:11:07 via Website

Hallo zusammen,

ich möchte mit ein BroadcastReceiver die Information von der Spotify auslesen.

Nun wird kein Wert an die MainActivity übertragen, somit bleibt das Textview leer.

Der Code in der BroadcastSpotify.java ist von Spotify selber

BroadcastSpotify.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.startService(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)) {


        // Sent only as a notification, your app may want to respond accordingly.
    }
}

}

MyService.java

public class MyService extends IntentService {

public MyService() {
    super("MyService");
}

public void onCreate() {
    super.onCreate();
    Log.i("sportify Server", ">>>onCreate() spotify");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, startId, startId);
    Log.i("spotify LocalService", "Received start id " + startId + ": " + intent);

    return START_STICKY;
}

@Override
protected void onHandleIntent(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, MainActivity.class);
    myIntent.putExtra("TrackName",trackName);
    myIntent.putExtra("AlbumName",albumName);
    startActivity(myIntent);

MainActivity

public class MainActivity extends AppCompatActivity {

public static TextView mTextViewResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mTextViewResult = (TextView) findViewById(R.id.google);






    Intent intent = getIntent();
  String  UserId=intent.getStringExtra("TrackName");
  String UserName=intent.getStringExtra("AlbumName");

Toast.makeText(getApplicationContext(),UserId,Toast.LENGTH_SHORT).show();

Manifest

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

        <intent-filter>

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

        </intent-filter>



    </receiver>

    <service android:enabled="true" android:name="de.janoroid.spotifybroadcast.MyService" >
    </service>
Diskutiere mit!
Beste Antwort
  • Forum-Beiträge: 3.501

18.01.2020, 10:24:51 via Website

Folgendes stand auf der Spotify Developer Webseite:
If you are developing an Android application and want to know what is happening in the Spotify app, you can subscribe to broadcast notifications from it.

Und dafür musst du erst die Client ID deines Packages registrieren/erhalten

Was genau macht den die Remotelibrary?

https://developer.spotify.com/documentation/android/

Liebe Grüße - Stefan
[ App - Entwicklung ]

Hilfreich?
Diskutiere mit!
  • Forum-Beiträge: 3.501

18.01.2020, 09:00:48 via Website

Hallo,

a) mir ist nicht klar , was du mit dem Code erreichen möchtest.
Die Activity würde einmal nur etwas bekommen, der nächste Track bliebe unbeachtet.

b) Was sagt denn dein Step/Step Debugging ?
c) Ist dein Package auch registriert ? ( ClientID/RedirectUri )
d) Gibt es einen Grund, warum du nicht direkt die RemoteLibrary verwendest?

— geändert am 18.01.2020, 09:34:26

Liebe Grüße - Stefan
[ App - Entwicklung ]

Hilfreich?
Diskutiere mit!
  • Forum-Beiträge: 63

18.01.2020, 10:03:26 via Website

Hallo swa00,

Folgendes stand auf der Spotify Developer Webseite:

If you are developing an Android application and want to know what is happening in the Spotify app, you can subscribe to broadcast notifications from it.

Der Code von der BroadcastSpotify.java stammte von Spotify selber.

Ich möchte einfach von der Spotify App erfahren,was momentan abgespielt wird.

Was genau macht den die Remotelibrary?

Die Activity würde einmal nur etwas bekommen, der nächste Track bliebe unbeachtet.

Ich wäre erst mal glücklich, wenn überhaupt was im Textview angezeigt wird.

Gruß,

Hilfreich?
Diskutiere mit!
Beste Antwort
  • Forum-Beiträge: 3.501

18.01.2020, 10:24:51 via Website

Folgendes stand auf der Spotify Developer Webseite:
If you are developing an Android application and want to know what is happening in the Spotify app, you can subscribe to broadcast notifications from it.

Und dafür musst du erst die Client ID deines Packages registrieren/erhalten

Was genau macht den die Remotelibrary?

https://developer.spotify.com/documentation/android/

Liebe Grüße - Stefan
[ App - Entwicklung ]

Hilfreich?
Diskutiere mit!
  • Forum-Beiträge: 63

18.01.2020, 11:28:26 via Website

Das mit der Anmeldung habe ich so gelöst.

 @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
        }


    }
}

}

Ich will auch nur die Information von den Künstler haben, mehr will nicht

Gruß,

Danke für deine Mühe

Hilfreich?
Diskutiere mit!
Empfohlene Artikel bei NextPit