1
我想創建一個主屏幕快捷方式到我的應用程序的內部活動。 This答案幫助我開始。Android:主屏幕快捷方式與可序列化的額外
這裏的基本代碼:
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是一個序列化對象沒有。所以現在我將對象的所有字段都作爲字符串提供,並在調用活動時重新創建對象。這確實有用,但是它很麻煩而且代碼很髒。
任何想法爲什麼提供一個可序列化的對象在這種情況下不起作用?謝謝。
更新:這是我如何嘗試恢復可序列化:
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;
...
}
}
你的stationData是否可擴展?你也可以提供你如何獲得額外的代碼? – PedroAGSantos
是的。串行化如果不用於快捷方式,效果很好。 – Androidicus
當你檢索serialisable時你能提供代碼嗎? – PedroAGSantos