【问题标题】:How to save user selected intents?如何保存用户选择的意图?
【发布时间】:2014-07-16 00:25:50
【问题描述】:

所以我正在构建一个启动器,我需要一种保存 Intent 的方法。 我正在使用以下代码获取已安装应用程序的列表,用户可以在其中选择要运行的应用程序。

代码-

         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"));

但是我有几个按钮,我正在使用这种方法让用户选择要运行的应用程序。但问题是我希望保存用户所做的这些选择,例如,当我单击时钟按钮时,它让我选择要运行的应用程序,然后保存。但我不知道怎么做。

我也确实找到了一种方法,我只是删除了“Intent.createChooser();”,以便我可以选择“使用默认值”,但是当您将其设置为默认值时,您按下的任何其他按钮(例如日历按钮),它打开时钟按钮。

我已经对意图进行了大量研究并且已经完成了所有这些工作,但我发现上面很难做到这一点,如果你知道制作应用选择器的方法,用户可以在其中选择要使用的应用按下按钮时启动,然后保存该意图,因此用户不必继续选择。

感谢您抽出宝贵时间,希望我已经很好地询问了所有问题。

【问题讨论】:

标签: java android eclipse class android-intent


【解决方案1】:

我建议将用户想要的应用程序包名称保存为默认值。

为此,您应该将Intent.createChooserstartActivity 的调用分开,然后在这两个调用之间,将包保存在意图中。例如:

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(...)

【讨论】:

  • 感谢您的帮助,但我遇到了很多错误。
  • @Robin - 这是因为这不是完整代码 - 您需要使用方法作为参数的任何内容填写 ...,就像您在问题中的代码一样
  • 谢谢,我更改了代码并将您的示例用作参考,但我得到了 saveChoice(动作,包名);
  • @Robin - 是的 - 你需要编写一个新方法来保存选择(我也省略了) - 我建议查看共享首选项来执行此操作 - stackoverflow.com/questions/3624280/…
猜你喜欢
  • 2015-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
相关资源
最近更新 更多