每个选择器无法使用Chooser进行一次startActivity()询问 [英] Cannot make startActivity() with Chooser ask just once per app

查看:120
本文介绍了每个选择器无法使用Chooser进行一次startActivity()询问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用选择器执行startActivity()时,Android会列出所有有权处理您的Intent的应用,以及用于永久或一次性设置此分配的选项(在ICS上,其AlwaysJust once操作按钮,在2.x上是一个复选框).但是对于此代码:

When you do startActivity() with chooser, Android would list all apps entitled to handle your Intent along with options to set this assignment permanent or once-time (on ICS its Always and Just once action button, on 2.x it's acheckbox). However for this code:

public class Redirector {
    public static void showActivityWithChooser( Context context, int chooserLabelTitleId, Intent intent ) {
      try {
        context.startActivity( Intent.createChooser( intent, 
                     context.getResources().getString( chooserLabelTitleId )) );
      } catch( Exception e ) {
        e.printStackTrace();
      }
    }

    public static void viewInExternalApplication( Context context, String url ) {
      Intent intent = new Intent(   Intent.ACTION_VIEW );
      intent.setData( Uri.parse( url ) );
      intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET );
      showActivityWithChooser( context, R.string.open_chooser_title, intent );
    }
}

我看不到任何Always | Just once按钮,并且无法使我的选择永久生效.我忽略了哪些基本知识导致Android无法使用户选择持久化?

I see no Always | Just once buttons and cannot make my selection permanent. What elementary I overlooked that made Android unable to make user choice persistent?

查看图片:左边的对话框是我想要看到的,而右边是我现在所看到的(两个对话框中应用程序的数量无关紧要):

See the pics: left dialog is what I'd like to see, but right is what I get now (different number of applications in both dialogs is irrelevant):

推荐答案

为了记录-它是(我的)错误的过度解释类型.我使用的选择器恰好可以在右侧的图像上看到.它一直在显示,因为...我一直在打电话.我错误地认为选择器提供Always | Just once功能,如果用户点击始终",则不会出现. (如果他/她使用了Just once,则会显示该信息).但这是错误的.选择器将始终显示,因为这是它的作用-让用户选择.

For a record - it was over-interpretation type of bug (of mine). The chooser I was using is exactly what can be seen on the image on the right side. And it was showing up all the time because... I was calling it all the time. I incorrectly assumed that chooser offers Always | Just once functionality and would not show up if user tapped "Always" (and will show up if s/he used Just once). But it is wrong. Chooser will always show up because that's its role - to let user choose.

Always | Just once功能与众不同->它是startActivity()(和startActivityForResult()等)的Android框架的功能,并且会在需要时自动显示(当有多个应用,或者确切地说,在应用清单中声明了多个匹配的intent-filter时,与某些Intent匹配).如果只有一个(或者用户已经选择Always),则不会显示.

The Always | Just once functionality is different thing -> it is a feature of the Android framework for startActivity() (and startActivityForResult() etc) and it would show up automatically when needed (when there's more than one app, or precisely, more than one matching intent-filter declared in app manifest, that matches certain Intent). It would not show up if you got just one (or if user already choose Always).

因此,作为开发人员,您无需关心或以任何特殊方式处理案件.要解决此问题,我只需将我的viewInExternalApplication()代码更改为仅调用startActivity():

So you, as developer do not need to care nor handle the case in any special way. To fix the issue I simply changed my viewInExternalApplication() code to just call startActivity():

try {
  context.startActivity( intent );
} catch (.... )

,让框架完成其余工作.

and let the framework do the rest.

这篇关于每个选择器无法使用Chooser进行一次startActivity()询问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆