How to get default application for an action

后端 未结 2 996
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 23:45

How can I determine which application is the default application for a certain action? For example I want to know which application is used for making calls or receiving text me

相关标签:
2条回答
  • 2021-01-21 23:59

    resolveActivity does something along the lines of what you're looking for. From the official docs:

    Determine the best action to perform for a given Intent. This is how resolveActivity(PackageManager) finds an activity if a class has not been explicitly specified.

    And here's an example:

    Intent i = (new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
    PackageManager pm = context.getPackageManager();
    final ResolveInfo mInfo = pm.resolveActivity(i, 0);
    Toast.makeText(
        context, 
        pm.getApplicationLabel(mInfo.activityInfo.applicationInfo),
        Toast.LENGTH_LONG
    ).show();
    

    Note that the return value is somewhat fuzzy:

    Returns a ResolveInfo containing the final activity intent that was determined to be the best action. Returns null if no matching activity was found. If multiple matching activities are found and there is no default set, returns a ResolveInfo containing something else, such as the activity resolver.

    0 讨论(0)
  • 2021-01-22 00:06

    Use Intent Filters and resolveActivity().

    From Android's documentation on Intent Filters:

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
    sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type
    
    ComponentName compName = sendIntent.resolveActivity();
    

    And here's the documentation on ComponentName

    0 讨论(0)
提交回复
热议问题