上次遇到 Intent 使用 Parcel 序列化出錯(見 https://www.linuxidc.com/Linux/2014-02/96336.htm),一直找不出原因,加上專案緊急,只好另尋他法。
查看 Intent 的源碼後發現,這個類別本身已經實作了序列化功能。
序列化
intent.toURI();
反序列化則使用:
Intent.parseUri(uriString, 0);
先看序列化:
intent.toURI();
Intent intent = new Intent("cn.eben.bookshelf.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String intnetUri = intent.toURI();
//序列化後:
//#Intent;action=cn.eben.bookshelf.VIEW;launchFlags=0x10000000;end
反序列化使用:
Intent.parseUri(uriString, 0);
Intent i;
try {
i = Intent.parseUri(uriString, 0);
context.startActivity(i);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
簡單又方便,上個問題就此解決。
更多Android相關信息見Android 專題頁面 https://www.linuxidc.com/topicnews.aspx?tid=11