Keeping Android intents?

I am creating a custom setting to remember what activity the user wants to launch from a list of possibilities. (By effectively implementing the "always run this activity when you see this intent" Chooser, but specifically remembering the result for my particular application, not the system-wide version.)

I get a list of possible actions like this:

    Intent myIntent = new Intent();
    myIntent.putExtra(Intent.EXTRA_TEXT, "Probe text");

    PackageManager manager = getContext().getPackageManager();
    List<ResolveInfo> infoList = manager.queryIntentActivities(myIntent,PackageManager.MATCH_DEFAULT_ONLY);


... results in a list of ResolveInfo s.

My question is, once my user has selected one of these, what's the best way to store this as a preference? That is, what am I writing to my SharedPreferences (in one element if possible) and how on the next launch of my application do I read this and fire the corresponding Intent?


source to share

1 answer

Well, you can call toUri()

on Intent

to have it converted to Uri

, which you can then convert to String

via standard toString()

. To expand the process, analyze String

in Intent

using Intent.parseUri()


However, you will need to add sufficient protection to deal with various possibilities, such as:

  • user uninstalled app
  • the user has updated the app and the app no ​​longer supports the Intent

    one you saved

Also, be careful when you insert this probe (text). :-)



All Articles