2012-08-14 116 views
2

我有一個應用程序,我可以啓動安裝在我的手機上的其他應用程序,只需長時間點擊我就可以獲取應用程序選擇器,結果我收到一個意圖數據,我如何保存它以便用戶當關閉回來我的應用程序有相同的快捷方式設置?保存意向共享首選項

我省其他像這樣的事情

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
    SharedPreferences.Editor editor = settings.edit(); 
    editor.putInt("Counter1", counter); 
    editor.putBoolean("FirstRun", firstRun); 
    editor.putString("Label2", label2S); 

    editor.commit(); 

但我不能與意圖

回答

0

做同樣的你可以序列化對象的字符串,並保存在首選項生成的字符串。一個簡單的方法是以json格式對它進行序列化,例如使用Google Gson

+0

我不熟悉GSON,你可以給我提供的方法嗎?我看着如何將其轉換爲uri,而不是回到intent,但我找不到任何東西,我發現這個https://groups.google.com/forum/?fromgroups#!topic/android-developers/cffRLznyto0% 5B1-25%5D 但它不起作用,我可以保存它,但我不能使用getIntent,因爲它表示它已被棄用 – DoubleP90 2012-08-14 16:28:15

5

好吧,我發現了一種 我保存這樣

SharedPreferences settings = getSharedPreferences(PREFERENCES, 0); 
       SharedPreferences.Editor editor = settings.edit(); 
       String uriString = data.toUri(requestCode); 
       editor.putString("Contacts_app", uriString); 
       editor.commit(); 

意圖然後,我檢索它像這樣

SharedPreferences settings = getSharedPreferences(PREFERENCES, 0); 
    String contactsApp = settings.getString("Contacts_app", null); 
    try { 
     telApp = Intent.parseUri(contactsApp, 0); 
    } catch (URISyntaxException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }