我试图将通过Spotify API(SpotifyAppRemote实例)创建的对象从我的MainActivity类发送到BackgroundService(实现为IntentService)。 由于我无法控制API,因此无法使用parcelable发送对象,因此我试图使用GSON从我的意图通过putExtra方法发送它,如下所示:
intent.putExtra("spotifyRemote", gson.toJson(mSpotifyAppRemote, SpotifyAppRemote.class));
但是,在运行时出现错误:
java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: com.spotify.protocol.types.ImageUri. Forgot to register a type adapter?
还有其他方法可以将此对象发送到我的服务吗?寻找此错误消息并没有真正帮助。
这是我的MainActivity类中的代码:
@Override
public void onConnected(SpotifyAppRemote spotifyAppRemote) {
mSpotifyAppRemote = spotifyAppRemote;
getCurrentTrack();
// Now you can start interacting with App Remote
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
infoText.setText("Successfully started!");
counter = 1;
numSongs = Integer.parseInt(mEdit.getText().toString());
PlayerApi playerApi = mSpotifyAppRemote.getPlayerApi();
playerApi.seekTo(0);
playerApi.resume();
Intent intent = new Intent(MainActivity.this, BackgroundService.class);
intent.putExtra("counter", counter);
intent.putExtra("numSongs", numSongs);
intent.putExtra("firstTrack", gson.toJson(curTrack, Track.class));
intent.putExtra("spotifyRemote", gson.toJson(mSpotifyAppRemote, SpotifyAppRemote.class));
startService(intent);
}
});
}
答案 0 :(得分:0)
为此,我在Service中声明了一个静态变量,并在MainActivity中设置了该变量,如下所示:
BackgroundService.mSpotifyAppRemote = mSpotifyAppRemote;
我的服务中的声明:
public static SpotifyAppRemote mSpotifyAppRemote = null;