拦截/获取数据发送到Android上的Spotify Widget

时间:2012-12-31 12:38:47

标签: android widget broadcastreceiver spotify

我有Spotify并希望在我的应用程序中使用标题和歌曲名称。 (最终目标是将其导出到我的Liveview手表,以便我可以用它来控制Spotify)

由于存在Spotify-Widget,我只想添加一个广播接收器以将数据发送到小部件。

所以我添加了一个广播接收器,据我理解这个概念,这应该足够了,然后我应该能够获得Spotify发送到小部件的数据。

根据文档,您可以使用Widget Manager对广播接收器执行所有操作,因此我认为广播接收器没问题。

一旦调用on_receive,我就创建了一个Debug log Entry和一个Toast, 所以应该至少某事

从错误消息我知道如果你没有小部件, Spotify总是试图将数据发送给它, 所以我很确定,错误就在我身边。

我的守则如下:

(为简洁起见,以后简称为服务或其他东西,暂时) textview稍后将显示标题

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();

                }
    };

完整性,这是我的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:我知道一旦内部数据格式发生变化,我的应用程序就会崩溃, 因为它仅供私人使用,我不介意,所以请不要讨论那个。 如果 WAS 是一个API,我会接受它,因为没有......

0 个答案:

没有答案