开发者

Starting a specific app in Android

开发者 https://www.devze.com 2023-02-05 18:46 出处:网络
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).

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). H开发者_Go百科ow to do this?


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)


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号