2

I try to get a launch intent from a third party package from the PackageManager. According to the documentation (http://developer.android.com/reference/android/content/pm/PackageManager.html#getLaunchIntentForPackage(java.lang.String)), it throws a PackageManager.NameNotFoundException if the given package name cannot be found on the system and it returns null if the package does not contain a launch activity.

When I try to catch the exception, I get a compilation error:

    PackageManager manager = getPackageManager();
    try{
        Intent launchAppIntent = manager.getLaunchIntentForPackage("somePackageName");
        //...
    } catch (PackageManager.NameNotFoundException exception){

    }

java: exception android.content.pm.PackageManager.NameNotFoundException is never thrown in body of corresponding try statement

I think that one possibility might be that the behavior of that method changed and it returns always null instead of throwing an exception. But then, should I still wrap a try block around the call?

How should I proceed solving that issue?

I am currently building against Android 4.2.2.

4

1 回答 1

6

文档已过时。

“throws NameNotFoundException”已从Android 1.6(API 级别 4)中的方法中删除。

从 Android 4.0 开始, ApplicationPackageManager.java中的当前实现也不会引发任何异常。

您可以安全地删除 try catch。

PS:我知道,答案有点延迟,但我偶然发现了同样的问题,所以为什么不在这里分享。

于 2014-01-30T14:17:42.610 回答