Android Android development: Get a list of apps that can be shared on the phone

1. Configuration items

In the AndroidManifest.xml configuration file, add the following content:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    //...

    <queries>
        <intent>
            <action android:name="android.intent.action.SEND"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>

    //...
</manifest>

Note the following in the above configuration items:

<data android:mimeType="text/plain"/>

The mimeType can be adjusted to other values ​​as appropriate:

text/plain

image/png

*/*

Wait, not list them one by one

Two, Java implementation

With the aforementioned configuration, you can get it directly through the code:

public List<ResolveInfo> getShareApps(Context context) {
    List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
    Intent intent = new Intent(Intent.ACTION_SEND, null);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setType("text/plain");
    PackageManager pManager = context.getPackageManager();
    mApps = pManager.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
    return mApps;
}

The obtained collection, the elements of which are examples of the ResolveInfo class, can obtain the desired content through the following methods:

resolveInfo.activityInfo.name //launcher class名称
resolveInfo.activityInfo.packageName //package包名
resolveInfo.loadLable(context.getPackageManager()).toString() //应用名
resolveInfo.loadIcon(context.getPackageManager()) //图标

Guess you like

Origin blog.csdn.net/freezingxu/article/details/125725748