3

I know how to load a PDF file in Android. But if more than one PDF viewers are installed, Android shows a list to choose from. I want to load my PDF file with a specific PDF viewer (say DroidReader). How to do this?

Cristian
  • 198,401
  • 62
  • 356
  • 264
Mudassir
  • 13,031
  • 8
  • 59
  • 87

2 Answers2

2

I would strongly recommend not specifying an explicit class name in the Intent as the accepted answer recommends, since that is an implementation detail of the app that can change at any time on you.

Instead, build your Intent like normal, but use Intent.setPackage() to specify the system should only look in the desired app's package name for matching activities. That is:

Intent intent = new Intent(Intent.ACTION_VIEW, uriToView);
intent.setPackage("com.package.name.of.droidreader");
startActivity(intent)
hackbod
  • 90,665
  • 16
  • 140
  • 154
1

Then specify the complete name of the activity:

    Intent intent = new Intent();
    ComponentName comp = new ComponentName("com.package.name.of.droidreader", "com.package.name.of.droidreader.DroidReader");
    intent.setComponent(comp);
    startActivity(intent);

To know what the package name and activity are, you could take a look at the adb logcat output: when you open an activity it gets logged there. And, of course, configure the intent correctly so that the DroidReader know what file to open.

Lastly, but important, you should surround the startActivity method with a try-catch block catching the ActivityNotFoundException (I'm sure that most of the handsets won't have that specific app).

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Thank for the quick response, Cristian. Let me check it out. – Mudassir Jan 18 '11 at 05:22
  • @Cristian: Please tell me how to show a page fit in window in DroidReader? Do I have to pass the extras? I've already posted the question, but no reply yet. http://stackoverflow.com/questions/4720443/passing-extras-to-droidreader – Greenhorn Jan 18 '11 at 05:32
  • You will have better luck if you ask the DroidReader authors directly. Maybe they even didn't put anything like that and you are wasting your time. – Cristian Jan 18 '11 at 05:36
  • Hey Cristian, do ya know any other PDF viewer which supports this feature? – Greenhorn Jan 18 '11 at 05:39
  • 1
    Note -- please see my answer below. This is *not* a good suggestion, since it ties you to implementation details of the target app that could change at any time and break your code. – hackbod Jan 18 '11 at 05:44