Intercept / Get Data sent to Spotify Widget on And

2019-07-01 16:48发布

问题:

I have Spotify and want to use the title and Song name in my Application. (Final Goal would be to export it to my Liveview watch, so that I can contron Spotify with it)

As there is a Spotify-Widget, I simply would like to add a Broadcast Recevier to get the Data sent to the widget.

So I add a Broadcast receiver, and as far as I understand the concept, this should suffice and I then should be able to get the data Spotify sends to the widget.

According to the Documentation, you can do everything with a Broadcast Receiver you could do with a Widget Manager, so I think a Broadcast Receiver is fine.

I made a Debug log Entry, and a Toast, as soon as the on_receive is called, so there should be at least something.

From the error messages I know that if you don't have the widget, Spotify always trys to send the data to it, so I am quite sure, that the mistake is on my side.

My Code as Follows:

(shortened for brevity, later to be as a service or something, for now) The textview shall later show the Title

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // für den Song

    IntentFilter iF = new IntentFilter();


    iF.addCategory("ComponentInfo");
    iF.addCategory("com.spotify.mobile.android.service.SpotifyIntentService");
    iF.addCategory("com.spotify.mobile.android.service.SpotifyService");


    iF.addAction("com.spotify.mobile.android.ui.widget.SpotifyWidget");
    iF.addAction("ComponentInfo");
    iF.addAction("com.spotify");
    iF.addAction("com.spotify.mobile.android.service.SpotifyIntentService");
    iF.addAction("com.spotify.mobile.android.service.SpotifyService");


    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.spotify.mobile.android.ui");

    registerReceiver(mReceiver, iF);

    // albumtext = (TextView) findViewById(R.id.albumText);

}

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

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

                    Log.d("onReceive launched", "kekse");

                            String action = intent.getAction();
                            String cmd = intent.getStringExtra("command");

                            String com = intent.getStringExtra("ComponentInfo");

                            Log.d("mIntentReceiver.onReceive ", action + " / " + cmd+ " / "+ com);

                            String artist = intent.getStringExtra("artist");
                            String album = intent.getStringExtra("album");
                            String track = intent.getStringExtra("track");
                            Log.d("Music",artist+":"+album+":"+track);

                            Toast.makeText(context, "Command : "+cmd+"\n Artist : "+artist+" Album :"+album+"Track : "+track+" " , Toast.LENGTH_SHORT).show();

                }
    };

For Completeness, here my my Manifest-File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.KarlheinzMeier.SpotifyController"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />


<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="de.KarlheinzMeier.SpotifyControllerActivity"
        android:label="@string/app_name" >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.ComponentInfo" />
    </intent-filter>

    <receiver android:name="com.spotify.mobile.android.ui.widget.SpotifyWidget" android:label="Kekse">
    <intent-filter>
        <action android:name="com.spotify.mobile.android.service.SpotifyService" />
         <action android:name="com.spotify.mobile.android.service.SpotifyIntentService" />
    </intent-filter>

</receiver>

    </activity>

</application>

</manifest>

PS: I know that as soon as the internal Data Format changes, my App will break, as it is for private use only, I don't mind, so please no discussions about that. If there WAS an API, I'd take it, as there is none ...