Save intent in shared preference

前端 未结 2 1634
予麋鹿
予麋鹿 2021-01-14 00:19

I have an application from which i can launch other apps installed on my phone, with a long click i get the app picker, in result i receive an intent data, how can i save it

相关标签:
2条回答
  • 2021-01-14 00:33

    You could serialize the object to a string, and save the resulting string in the preferences. An easy way would be to serialize it in json format, using Google Gson for example.

    0 讨论(0)
  • 2021-01-14 00:36

    Ok i found a way I save the intent like this

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

    Then i retrieve it like this

    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();
        }
    
    0 讨论(0)
提交回复
热议问题