Branching the Android Share Intent extras depending on which method they choose to share

前端 未结 1 996
鱼传尺愫
鱼传尺愫 2020-12-09 11:24

Simply want to share a dynamic text string + the URL to the app. The native Android share intent is setup correctly, and works perfect with Twitter, Gmail, etc. But, as many

相关标签:
1条回答
  • 2020-12-09 12:06

    found a solution, on this SO question asking for something else: https://stackoverflow.com/a/8550043/1938669

    the attempt posted my original question here was close. within that cycle of possible shareIntent List, you need to create a new share intent targeted at the specific sharing choice (like facebook or twitter)

    here is a final working solution that shares only a URL if facebook is choosen, otherwise shares the complete text string + url:

    public void shareIt(View view){
        //sharing implementation
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = "string of text " + txt_var + " more text! Get the app at http://someapp.com";
    
        PackageManager pm = view.getContext().getPackageManager();
        List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
        for(final ResolveInfo app : activityList) {
    
             String packageName = app.activityInfo.packageName;
             Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
             targetedShareIntent.setType("text/plain");
             targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "share");
             if(TextUtils.equals(packageName, "com.facebook.katana")){
                 targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://someurl.com");
             } else {
                 targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
             }
    
             targetedShareIntent.setPackage(packageName);
             targetedShareIntents.add(targetedShareIntent);
    
        }
    
        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share Idea");
    
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
        startActivity(chooserIntent);
    
    }
    
    0 讨论(0)
提交回复
热议问题