1

so I'm building a launcher and I need a way of saving Intents. I am using the following code to get a list of installed applications where the user can select which to run.

Code -

         final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
         final List pkgAppsList = getActivity().getPackageManager().queryIntentActivities( mainIntent, 0);
         startActivity(Intent.createChooser(mainIntent, "Choose your default app"));

But I have several buttons, and I'm using this method to let the user choose which application to run. But the problem is I would like these selections made by the user to be saved, for example, when I click the clock button it lets me choose which app I want to run, then it's saved. But I don't know how.

Also I did find a way to do it, I just removed "Intent.createChooser();" so that I can select "use default" but when you set it as default, any other button you press such as the calendar button, it opens the clock button.

I have done lots of research on intents and have gotten all this working but I'm finding it hard to do that above, please if you know a way of making an App Chooser, where the user gets to select which app to launch when a button is pressed and then that intent gets saved so the user doesn't have to keep choosing.

Thanks for your time, hopefully I've asked everything in good manner.

Robin
  • 577
  • 1
  • 7
  • 13

1 Answers1

0

I would recommend saving the package name of the app that the user wants as default.

To do this, you should separate the call for Intent.createChooser and startActivity, then between those two calls, save the package in the intent. For example:

Intent intent = Intent.createChooser(...);
String packageName = intent.getPackage();//you might have to play around with the name of the method here.... it is not clear from the docs whether this returns what you want
String action = intent.getAction();//this will return the action that the user wants to preform - this needs to be saved too, so you know when to display the choosing dialog again and when not to
saveChoice(action, packageName);
startActivity(...)
jcw
  • 5,132
  • 7
  • 45
  • 54
  • Thanks for the help, but I'm getting loads of errors. – Robin May 31 '14 at 03:35
  • @Robin - That's because this is not full code - you need to fill in the `...` with whatever the method takes as a parameter, just like you have in you code in the question – jcw May 31 '14 at 09:13
  • Thanks, I changed the code and used your example as a reference, but I am getting The method saveChoice(String, String) is undefined for the type new View.OnClickListener(){} on saveChoice(action, packageName); – Robin Jun 01 '14 at 08:47
  • @Robin - yes - you will need to write a new method to save the choice (I left that out too) - I would recommend taking a look at shared preferences to do this - http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – jcw Jun 01 '14 at 08:59