Android – Sharing data from your app using Intent.ACTION_SEND

Sharing data or content from an app to another is a common requirement but with a twist. So today I am going to discuss several type of content sharing in this article.

 

There are mainly three ways to share data which depends on requirement, let’s have a look –

  • Sharing to all the available apps using Intent.ACTION_SEND

    Intent.ACTION_SENDThis means your app can share content to all the available apps of an Android device those can accept your content, and to implement it you have to use the following code –

    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    share.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
    // Add data to the intent, the receiving app will decide
    // what to do with it.
    share.putExtra(Intent.EXTRA_SUBJECT,  "Enter your title here");
    share.putExtra(Intent.EXTRA_TEXT, "Enter your description here");
    activity.startActivity(Intent.createChooser(share, "Share"));
  • Sharing to a specific app

    In some cases we only wants to share content to a specific app like Facebook, Twitter,LinkedIn etc. To achieve this use the below mentioned code –

    String packageName = "com.facebook.katana";
    String fullUrl = "https://m.facebook.com/sharer.php?u=..";
    
            Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
            if (intent == null) {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(fullUrl));
                startActivity(i);
            } else {
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setClassName(packageName ,
                        "com.facebook.katana.ShareLinkActivity");
                sharingIntent.putExtra(Intent.EXTRA_TEXT, "your title text");
                startActivity(sharingIntent);
            }

    The above code is good to share to Facebook, Twitter or Linked like social apps, whereas if you want to share your app’s data to another app and you know the URI of the other app you can still share without mentioning the activity name using implicit intent similar to the following code –

    String packageName = "com.sample";
            Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
            if (intent == null) {
                intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("market://details?id=" + packageName));
            } else {
                String url = "sample://app/uri";
                try {
                    intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

    This code will also take care if your intended app is not installed

     

     

    Read also –

  • Sharing to specific apps using Intent.ACTION_SEND

    Now this is the rarely used requirement where developer only want to show only a few set of apps to share content from their app and to achieve this you have to write little lengthy code

    List<Intent> targetShareIntents=new ArrayList<Intent>();
        Intent shareIntent=new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        List<ResolveInfo> resInfos=getPackageManager().queryIntentActivities(shareIntent, 0);
        if(!resInfos.isEmpty()){
            System.out.println("Have package");
            for(ResolveInfo resInfo : resInfos){
                String packageName=resInfo.activityInfo.packageName;
                Log.i("Package Name", packageName);
                if(packageName.contains("com.twitter.android") || packageName.contains("com.facebook.katana") || packageName.contains("com.kakao.story")){
                    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, "Text");
                    intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                    intent.setPackage(packageName);
                    targetShareIntents.add(intent);
                }
            }
            if(!targetShareIntents.isEmpty()){
                System.out.println("Have Intent");
                Intent chooserIntent=Intent.createChooser(targetShareIntents.remove(0), "Choose app to share");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
                startActivity(chooserIntent);
            }else{
                System.out.println("Do not Have Intent");
            }
        }

    The above code will show Facebook, Twitter, Kakao app in the intent chooser to share content.

 

I hope this article will be helpful to all of you, so do not forget to share it with your friends and colleagues.

Happy coding!!!

Leave a Reply

Your email address will not be published. Required fields are marked *