5

What I want is to hide WebBrowser when opening PDF file in my application. I would like to show only PDF reader application, if not have then display alert message.

Now I can create AppChooser dialog but I don't know how to hide some application.

Thank you very much!

MaliEbbae
  • 373
  • 1
  • 2
  • 10

2 Answers2

10

You have to query for available Apps before starting the chooser. And you need to know something about the app you want to exclude. FOr example the packagename

Intent pdfIntent = ...;
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(pdfIntent, 0);

List<Intent> targetPDFIntents = new ArrayList<Intent>();
for (ResolveInfo currentInfo : activities) {
    String packageName = currentInfo.activityInfo.packageName;
if (!"pageToExclude".equals(packageName)) {
        Intent targetPdfIntent = new Intent(android.content.Intent.ACTION_VIEW, exportData);
        targetPdfIntent.setPackage(packageName);
        targetPDFIntents.add(targetPdfIntent);
    }
}

Intent chooserIntent = Intent.createChooser(targetPDFIntents.remove(0), "title");               
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetPDFIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);

With that you start a chooser with a list of explicit activity of a packages. And all of them can handle the IntentType pdfIntent which is created in the first line.

creaity
  • 650
  • 1
  • 6
  • 14
  • Finally I found a way to filter out my app's activity from the result. Thanks! – Sufian Apr 24 '14 at 11:16
  • Your answer helps me a lot too. But in this implementation there is one issue: at the bottom of chooser dialog there is no "system" checkbox for setting chosen app as default. Do you know how to create chooser dialog with implemented functionality and with this "default" checkbox? (Without, for example, storing data about previously chosen app in SharedPrefs etc.)? – krossovochkin Jul 08 '14 at 13:35
  • I'm not 100% sure, but I don't believe, that it is possible without storing it on your own. Because you start the chooser explicite from your code and let not android show the chooser. And as far as I know, the default setting is not available and setable by the app. But if someone does know how it will work, I'm looking forward to see how it works – creaity Jul 09 '14 at 16:27
  • 2
    This answer has 2 mistakes: "targetPdfIntent" should be almost identical to pdfIntent so it should use a copy-CTOR (almost, since it needs to target a specific activity) . Also, instead of "targetPdfIntent.setPackage" , it should be "targetPdfIntent.setComponent(new ComponentName(packageName, currentInfo.activityInfo.name));" , so that it will be able to handle the case of multiple activities within the same app that can handle this intent. Otherwise, a weird item of "Android System" will be shown. You can test it on Google-Plus app, for example, when sharing an image. – android developer Nov 12 '14 at 15:53
  • Too bad I didn't leave a link to the solution I used. Link to my answer on this topic can be found here - [How to exclude a specific application from ACTION_VIEW Intent?](http://stackoverflow.com/a/23268821/1276636). – Sufian Feb 12 '16 at 07:09
0

I think you have to try to open PDF Reader (like Abode one) using explicit intent, catching the exception then display your alert message.

lithos35
  • 250
  • 2
  • 9