• Start activity with App Chooser or not ?


    启动一个Activity,可以直接使用Intent,例如:

    1 Intent intent = new Intent(Intent.ACTION_SEND);
    2 ...
    3 startActivity(intent );
    Start an Activity with the Intent

     

                 (图1)

    如果该Intent对应了多个应用可以处理,会出现一个对话框让用户来确定使用哪一个应用来响应。如果每次执行这样的Action,用户都希望用同样的应用来处理,可以选中对话框底部的复选框“Use as default for this action”,避免每次都选择的麻烦。


    但是,如果一个ACTION可以被多个应用处理,但用户希望每次使用时使用不同的应用来响应。比如“分享”功能,这次的分享可能是想通过“新浪微博”,下一个的分享就可能是“微信”。遇到这种情况,我们就需要显示的使用一个App Chooser来处理。

     1 Intent intent = new Intent(Intent.ACTION_SEND);
     2 ...
     3 
     4 // Always use string resources for UI text.
     5 // This says something like "Share this photo with"
     6 String title = getResources().getString(R.string.chooser_title);
     7 // Create intent to show chooser
     8 Intent chooser = Intent.createChooser(intent, title);
     9 
    10 // Verify the intent will resolve to at least one activity
    11 if (intent.resolveActivity(getPackageManager()) != null) {
    12     startActivity(chooser);
    13 }
    Start activity with App Chooser

                  (图2)

    使用App Chooser,用户就不能指定默认的处理应用。


    之前一直没有搞清楚为什么使用App Chooser来启动一个Activity,还傻傻的认为跟直接使用Intent来startActivity相比只是多了一个可以定制title显示。通过上面的比较,相信你已经了解了为什么要使用App Chooser,以及什么情况下来使用App Chooser。

    PS:有事没事去Android的官方网站溜达溜达,总会有些或多或少的收获。百度快餐,少用为好。

  • 相关阅读:
    php自动跳转中英文页面
    如何让一个层关闭之后,就算刷新页面了也不显示。除非关闭页面再次打开
    彻底杜绝warning: Cannot add header information headers already sent in......
    大三了
    X牛人關於編程語錄
    C++虚函数表解析
    epoll:Edge or Level Triggered
    LRU和LFU的区别
    会话、进程组与僵死进程
    Effective C++学习笔记:确定基类有虚析构函数
  • 原文地址:https://www.cnblogs.com/haobo/p/3644722.html
Copyright © 2020-2023  润新知