views:

62

answers:

2

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(Intent.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;
        }
A: 

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.

CommonsWare
Ok, cool. I thought that I'd have to do it using intents and categories and stuff but I don't really understand them yet so I thought I'd try another way.So, to start the messaging app, should I pass an Intent with ACTION_SEND or something? I'd be grateful if you could explain it to me.
Espiandev
@Alex: " So, to start the messaging app, should I pass an Intent with ACTION_SEND or something?" -- no, to *send a message*, you use an `ACTION_SEND` `Intent`. There is no notion of "start the messaging app". See http://www.androidguys.com/2009/11/02/a-call-to-action-action_send-that-is/
CommonsWare
Ok, so there isn't a way to open the default messaging app full stop? Damn. Oh well, thanks for the help!
Espiandev
A: 

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);
cdhabecker