Android:Homescreen Shortcut with Serializable Extra

时间:2016-07-26 07:43:20

标签: android android-intent

我想为我的应用的内部活动创建一个主屏幕快捷方式。 This回答帮助我开始了。

以下是基本代码:

Intent shortcutIntent = new Intent(context.getApplicationContext(),
        StationMainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.setAction(Intent.ACTION_MAIN);

Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, stationData.getName());
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext(context.getApplicationContext(),
                R.drawable.ic_launcher));

addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
context.getApplicationContext().sendBroadcast(addIntent);

不幸的是,虽然它确实提供了额外的String类型,但如果我尝试为shortcutIntent提供可序列化的额外代码,代码似乎会失败。

    shortcutIntent.putExtra("StationId", (String) stationData.getId());

确实有效。但

    shortcutIntent.putExtra("StationData", stationData);

其中stationData是可序列化的对象。所以现在我将对象的所有字段都提供为字符串,并在调用活动时重新创建对象。这确实有效,但代码繁琐而且很脏。

知道为什么提供可序列化对象在这种情况下不起作用?谢谢。

更新:以下是我尝试检索Serializeable的方法:

    stationData = (StationData) intent.getSerializableExtra("StationData");

StationData如下所示:

public class StationData implements Serializable {
    private String id; 
    private String name; 
    ...

    public StationData(String id, String name, ....) {
      this.id = id; 
      this.name = name; 
      ...
    }
 }

1 个答案:

答案 0 :(得分:0)

当您尝试获取数据时尝试使用:

Bundle extras = getIntent().getExtras();
extras.getSerializable("StationData");
相关问题