Intent官方教程(4)用Intent构造应用选择框

Forcing an app chooser

  When there is more than one app that responds to your implicit intent, the user can select which app to use and make that app the default choice for the action. This is nice when performing an action for which the user probably wants to use the same app from now on, such as when opening a web page (users often prefer just one web browser) .

  However, if multiple apps can respond to the intent and the user might want to use a different app each time, you should explicitly show a chooser dialog. The chooser dialog asks the user to select which app to use for the action every time (the user cannot select a default app for the action). For example, when your app performs "share" with the ACTION_SEND action, users may want to share using a different app depending on their current situation, so you should always use the chooser dialog, as shown in figure 2.
              

              Figure 2. A chooser dialog.
  To show the chooser, create an Intent using createChooser() and pass it to startActivity(). For example:

 1 Intent sendIntent = 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 the chooser dialog
 8 Intent chooser = Intent.createChooser(sendIntent, title);
 9 
10 // Verify the original intent will resolve to at least one activity
11 if (sendIntent.resolveActivity(getPackageManager()) != null) {
12     startActivity(chooser);
13 }

  This displays a dialog with a list of apps that respond to the intent passed to the createChooser() method and uses the supplied text as the dialog title.

 

posted @ 2016-09-25 08:19  f9q  阅读(201)  评论(0编辑  收藏  举报