如何通过意图共享来共享哪个应用程序

时间:2019-06-12 10:34:56

标签: android

//share intent function
private void shareIntent(String link) {
try {
    //check permission is granted ot not
    if (ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
        PackageManager.PERMISSION_GRANTED) {
        List < Intent > shareIntentsLists = new ArrayList < Intent > ();
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, link);
        shareIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
        shareIntent.setType("image/*");
        List < ResolveInfo > resInfos = getActivity().getPackageManager().queryIntentActivities(shareIntent, 0);
        if (!resInfos.isEmpty()) {
            for (ResolveInfo resInfo: resInfos) {
                String packageName = resInfo.activityInfo.packageName;
                if ((!packageName.toLowerCase().contains("com.facebook.katana")) &&
                    (!packageName.toLowerCase().contains("com.mm.parentconcern")) &&
                    (!packageName.toLowerCase().contains("com.facebook.orca")) &&
                    (!packageName.toLowerCase().contains("com.linkedin.android"))) {
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
                    intent.setAction(Intent.ACTION_SEND);
                    intent.putExtra(Intent.EXTRA_STREAM, photoURI);
                    intent.putExtra(Intent.EXTRA_TEXT, link);
                    intent.setType("image/*");
                    intent.setPackage(packageName);
                    shareIntentsLists.add(intent);
                }
                if (packageName.contains("com.facebook.orca")) {
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
                    intent.setAction(Intent.ACTION_SEND);
                    intent.putExtra(Intent.EXTRA_TEXT, link);
                    intent.setType("image/*");
                    intent.setPackage(packageName);
                    if (!shareIntentsLists.contains(intent)) {
                        shareIntentsLists.add(intent);
                    } else {
                        shareIntentsLists.remove(intent);
                    }
                }
                if (packageName.contains("com.facebook.katana")) {
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
                    intent.setAction(Intent.ACTION_SEND);
                    intent.setType("text/plain");
                    intent.putExtra(Intent.EXTRA_TEXT, link);
                    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(link));
                    intent.setPackage(packageName);
                    if (!shareIntentsLists.contains(intent)) {
                        shareIntentsLists.add(intent);
                    } else {
                        shareIntentsLists.remove(intent);
                    }
                }
                if (packageName.contains("com.linkedin.android")) {
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
                    intent.setAction(Intent.ACTION_SEND);
                    intent.putExtra(Intent.EXTRA_TEXT, link);
                    intent.setType("text/*");
                    intent.setPackage(packageName);
                    if (!shareIntentsLists.contains(intent)) {
                        shareIntentsLists.add(intent);
                    } else {
                        shareIntentsLists.remove(intent);
                    }
                }

            }
            //this is share app list check
            if (!shareIntentsLists.isEmpty()) {
                Intent chooserIntent = Intent.createChooser(shareIntentsLists.remove(0),
                    getResources().getString(R.string.social_share_head));
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    shareIntentsLists.toArray(new Parcelable[] {}));
                //startActivity(chooserIntent);
                startActivityForResult(chooserIntent, 5);
            }

        }


    } else {
        //permissiom for storage
        askWriteExternalStoragePermission();
    }
} catch (Exception e) {
    e.printStackTrace();
}

}

1 个答案:

答案 0 :(得分:0)

不能一口气说出来,而是将鼠标悬停在ShareIntent上并单击,然后您将被重定向到其他页面,您将获得从那里发送的内容。