自定义Android Intent.ACTION_SEND

时间:2013-11-16 06:14:30

标签: android facebook android-intent

我正在使用Intent来共享网址和主题。在此意图过滤器中显示所有共享应用程序。我只想要(facebook / gmail / message / skype / twitter)弹出窗口中的这些选项。这可以自定义共享意图过滤器。

 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"));

感谢

4 个答案:

答案 0 :(得分:71)

是的,它可能在下面查看显示Facebook,Gmail,Twitter的过滤。

已更新为共享文字+图片:

从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);

通过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;
          }
        }

通过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;
        }
      }

通过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;
           }
       }

通过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;
                    }
                }

答案 1 :(得分:18)

多功能的单一方法

share(String nameApp,String imagePath,String message)功能代码:

public void share(String nameApp, String imagePath, String message) {
    try {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/jpeg");
        List<ResolveInfo> resInfo = getPackageManager()
                .queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
                Intent targetedShare = new Intent(
                        android.content.Intent.ACTION_SEND);
                targetedShare.setType("image/jpeg"); // put here your mime
                                                        // type
                if (info.activityInfo.packageName.toLowerCase().contains(
                        nameApp)
                        || info.activityInfo.name.toLowerCase().contains(
                                nameApp)) {
                    targetedShare.putExtra(Intent.EXTRA_SUBJECT,
                            "Sample Photo");
                    targetedShare.putExtra(Intent.EXTRA_TEXT, message);
                    targetedShare.putExtra(Intent.EXTRA_STREAM,
                            Uri.fromFile(new File(imagePath)));
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShareIntents.add(targetedShare);
                }
            }
            Intent chooserIntent = Intent.createChooser(
                    targetedShareIntents.remove(0), "Select app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    targetedShareIntents.toArray(new Parcelable[] {}));
            startActivity(chooserIntent);
        }
    } catch (Exception e) {
        Log.v("VM",
                "Exception while sending image on" + nameApp + " "
                        + e.getMessage());
    }
}

用于在gmail,facebook,twitter上附加图像,文字使用以下代码。

File filePath = new File("your image path");
share("gmail", filePath.toString(),"your comment");
share("facebook", filePath.toString(),"your comment");
share("twitter", filePath.toString(),"your comment");

答案 2 :(得分:2)

以上给出的答案在我的手机上打开Facebook聊天应用程序。对于以下工作!!

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                sharingIntent.putExtra(Intent.EXTRA_TEXT, "Text...");
                PackageManager packManager = getPackageManager();
                List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(sharingIntent,  PackageManager.MATCH_DEFAULT_ONLY);

                boolean resolved = false;
                for(ResolveInfo resolveInfo: resolvedInfoList){
                    if(resolveInfo.activityInfo.packageName.startsWith("com.facebook.katana")){
                        sharingIntent.setClassName(
                            resolveInfo.activityInfo.packageName, 
                            resolveInfo.activityInfo.name );
                        resolved = true;
                        break;
                    }
                }
                if(resolved){
                    startActivity(sharingIntent);
                }else{

                     Builder alert  = new AlertDialog.Builder(ActivityName.this);
                        alert.setTitle("Warning");
                        alert.setMessage("Facebook App not found");
                        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int which) 
                            {
                                dialog.dismiss();

                            }
                        });
                        alert.show();
                } 

答案 3 :(得分:0)

您无法在不使用facebook sdk的情况下与图片分享附加内容。

https://developers.facebook.com/x/bugs/332619626816423/