2012-08-14 118 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(); 
    }