获取可共享数据的应用程序列表

Him*_*sal 5 android android-package-managers

此代码显示默认共享对话框

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Message"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));
Run Code Online (Sandbox Code Playgroud)

分享对话

问题:我没有在默认系统对话框中显示应用程序列表,而是希望获取应用程序列表并在我的自定义列表中显示它们.

Swa*_*yam 6

将PackageManager与Intent一起使用以获取可以侦听SEND意图的应用程序列表.从返回的应用程序列表中,获取您想要显示的详细信息,例如.图标,名称等.当用户点击它时,您需要包名称来启动应用程序.

PackageManager pm = getActivity().getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_SEND, null);
mainIntent.setType("text/plain");
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, 0); // returns all applications which can listen to the SEND Intent
for (ResolveInfo info : resolveInfos) {
    ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;

    //get package name, icon and label from applicationInfo object and display it in your custom layout 

    //App icon = applicationInfo.loadIcon(pm);
    //App name  = applicationInfo.loadLabel(pm).toString();
    //App package name = applicationInfo.packageName;
}
Run Code Online (Sandbox Code Playgroud)

获得这组应用程序详细信息后,可以在GridView的适配器中使用它并显示详细信息.