开发者

Launching default apps with intents

开发者 https://www.devze.com 2023-01-12 14:03 出处:网络
How, in android, do I start an app set as the default (i.e. Handcent for Messaging, Dolphin for browsing)?

How, in android, do I start an app set as the default (i.e. Handcent for Messaging, Dolphin for browsing)?

I can only find how to use definite package names for intents:

Intent i = new Intent(Inten开发者_StackOverflow中文版t.ACTION_MAIN);
        i.addCategory(Intent.CATEGORY_LAUNCHER);

        switch (position) {
        case 0: //messages
            i.setPackage("com.android.mms");
            break;
        case 1: //inbox
            i.setPackage("com.android.email");
            break;
        case 2: //browser
            i.setPackage("com.android.browser");
        default:
            i = null;
        }


How, in android, do I start an app set as the default (i.e. Handcent for Messaging, Dolphin for browsing)?

"Default" is for a specific operation (e.g., sending a message), not for some abstract notion of "Messaging" in general.

Also, the code you are showing above uses things that are not in the SDK (namely, specific packages). Your code will break on some devices, where the device manufacturer has replaced the app. Your code may break in future versions of Android, when the stock apps are refactored or otherwise renamed.

I think you need to reconsider what it is you are trying to accomplish.


You can search for apps that satisfy a given Intent (e.g., ACTION_SEND), decide which one you want, retrieve its component name, and then launch it with a different Intent that specifies the component name.

Start with:

Intent intent = new Intent(...);
List<ResolveInfo> list = getPackageManager().queryIntentActivities(
    intent, PackageManager.MATCH_DEFAULT_ONLY);
0

精彩评论

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