无法从服务意图中获取自定义对象

时间:2022-09-06 20:47:24

I am creating a music player. When I send service info and playlist in custom object to MyService in intent, MyService ca not get it from intent and returns null

我正在创造一个音乐播放器。当我将自定义对象中的服务信息和播放列表发送到意图中的MyService时,MyService无法从intent获取它并返回null

sending intent to Myservice:

向Myservice发送意图:

Intent intent = new Intent(getActivity(), MyService.class);
                intent.putExtra("command", list.get(position));
                intent.putExtra("dataBase", serviceInfo);
                getActivity().startService(intent);`

trying to get service info in MyService from intent

试图从意图中获取MyService中的服务信息

serviceInfo = (SettingsAndPlaylist) intent.getSerializableExtra("dataBase");
            position = serviceInfo.getPosition();
            repeatController = serviceInfo.isRepeat();
            playList = serviceInfo.getPlayList();

object with settings and playlist is serealizable and not null in the sending intent

具有设置和播放列表的对象是可实现的,并且在发送意图中不为空

player sources

2 个解决方案

#1


0  

Well, your serializable class reads:

好吧,你的可序列化类读取:

public class SettingsAndPlaylist implements Serializable {
    private static List playList;
    private static int position;
    private static boolean repeat;
    private static boolean random;

Serialization works for an object instance fields, not static fields. By the way, you should also declare a serialVersionUID.

序列化适用于对象实例字段,而不适用于静态字段。顺便说一下,你还应该声明一个serialVersionUID。

See http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html for details about both points.

有关这两点的详细信息,请参见http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html。

#2


0  

Not sure what flags you return from onStartCommand, have you tried different flags, like START_REDELIVER_INTENT?

不确定你从onStartCommand返回什么标志,你尝试过不同的标志,比如START_REDELIVER_INTENT吗?

If your are using START_STICKY you need to be prepared for null intents.

如果您正在使用START_STICKY,则需要为null意图做好准备。

#1


0  

Well, your serializable class reads:

好吧,你的可序列化类读取:

public class SettingsAndPlaylist implements Serializable {
    private static List playList;
    private static int position;
    private static boolean repeat;
    private static boolean random;

Serialization works for an object instance fields, not static fields. By the way, you should also declare a serialVersionUID.

序列化适用于对象实例字段,而不适用于静态字段。顺便说一下,你还应该声明一个serialVersionUID。

See http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html for details about both points.

有关这两点的详细信息,请参见http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html。

#2


0  

Not sure what flags you return from onStartCommand, have you tried different flags, like START_REDELIVER_INTENT?

不确定你从onStartCommand返回什么标志,你尝试过不同的标志,比如START_REDELIVER_INTENT吗?

If your are using START_STICKY you need to be prepared for null intents.

如果您正在使用START_STICKY,则需要为null意图做好准备。