Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

facebook - Customize Android Intent.ACTION_SEND

I'm using Intent for sharing url and subject. In this intent filter showing all the sharing apps. i want only (facebook/gmail/message/skype/twitter) these option in popup. is this possible to customize sharing intent filter.

 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
 sharingIntent.setType("text/plain");
 String shareBody = adapter.getDetails("url";
 sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"subject");
 sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
 startActivity(Intent.createChooser(sharingIntent, "Share via"));

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, its possible Check out below which shows the filteration for Facebook,Gmail,Twitter.

Updated to Share Text + Image:

Select the image from the SDCard:

String fileName = "image-3116.jpg";
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
String myDir = externalStorageDirectory + "/saved_images/"; // the
            // file will be in saved_images
Uri uri = Uri.parse("file:///" + myDir + fileName);

Share via Twitter

  Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
   shareIntent.setType("text/plain");
   shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
   shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

   PackageManager pm = v.getContext().getPackageManager();
   List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
     for (final ResolveInfo app : activityList) 
      {
        if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
          {
             final ActivityInfo activity = app.activityInfo;
             final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
             shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
             shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
             shareIntent.setComponent(name);
             v.getContext().startActivity(shareIntent);
            break;
          }
        }

Share via Facebook

   Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
   shareIntent.setType("text/plain");
   shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
   shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
   shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

   PackageManager pm = v.getContext().getPackageManager();
   List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
     for (final ResolveInfo app : activityList) 
     {
         if ((app.activityInfo.name).startsWith("com.facebook.katana")) 
         {
           final ActivityInfo activity = app.activityInfo;
           final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
          shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
          shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
          shareIntent.setComponent(name);
          v.getContext().startActivity(shareIntent);
          break;
        }
      }

Share via Gmail

  Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
  shareIntent.setType("text/plain");
  shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
  shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

   PackageManager pm = v.getContext().getPackageManager();
   List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
       for (final ResolveInfo app : activityList) 
        {
          if ((app.activityInfo.name).contains("android.gm")) 
           {
             final ActivityInfo activity = app.activityInfo;
             final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
            shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
             shareIntent.setComponent(name);
             v.getContext().startActivity(shareIntent);
             break;
           }
       }

Share via WhatsApp:

 Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
 shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
        for (final ResolveInfo app : activityList) {
            if ((app.activityInfo.name).contains("com.whatsapp")) {
                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(
                                activity.applicationInfo.packageName, activity.name);
                          shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        shareIntent.setComponent(name);
                        v.getContext().startActivity(shareIntent);
                        break;
                    }
                }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...