ShareActionProvider Android系统菜单

时间:2015-05-29 11:08:44

标签: android menu

我的菜单中有一点问题。我点击我的MenuItem,然后分享,我想打开我的意图(选择器)。但我之间的android系统项目。

我的代码:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        MenuItem item = menu.findItem(R.id.file);
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);

        setShareIntent();
        return true;
    }


    private void setShareIntent() {

        Resources resources = getResources();

        Intent emailIntent = new Intent();
        emailIntent.setAction(Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
        emailIntent.setType("message/rfc822");

        PackageManager pm = getPackageManager();
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("text/plain");

        Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));

        List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
        List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
        for (int i = 0; i < resInfo.size(); i++) {

            ResolveInfo ri = resInfo.get(i);
            String packageName = ri.activityInfo.packageName;
            if(packageName.contains("android.email")) {
                emailIntent.setPackage(packageName);
            } else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("text/plain");
                if(packageName.contains("twitter")) {
                    intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
                } else if(packageName.contains("facebook")) {
                    intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
                } else if(packageName.contains("mms")) {
                    intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
                } else if(packageName.contains("android.gm")) {
                    intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
                    intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
                    intent.setType("message/rfc822");
                }

                intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
            }
        }

        LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);

        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);

        mShareActionProvider.setShareIntent(openInChooser);
        //startActivity(openInChooser);
    }

这条线路? :

mShareActionProvider.setShareIntent(openInChooser);

我没看到。

我的菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.app.FragmentsSliderNews" >

    <item android:id="@+id/file"
        android:title="Share"
        android:icon="@drawable/ic_action_share"
        app:actionProviderClass=
            "android.support.v7.widget.ShareActionProvider">
    </item>
</menu>

谢谢大家的答案。

3 个答案:

答案 0 :(得分:0)

摆脱createChooser()。您只能将其与startActivity()一起使用,而不能用于ShareActionProvider

答案 1 :(得分:0)

通过Android中的选项分享

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "News for you!");
intent.putExtra(android.content.Intent.EXTRA_TEXT,"Additional text");
startActivity(Intent.createChooser(intent, "Share via"));

**它可能在下面查看显示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);
enter code here

通过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).contains("facebook")) 
     {
       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;
       }
   }

答案 2 :(得分:0)

没关系,我改变了我的代码:

&#13;
&#13;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        MenuItem item = menu.findItem(R.id.file);
        
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
        
        mShareActionProvider.setOnShareTargetSelectedListener(this);

        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                openShare();
                return false;
            }
        });
        
        return true;
    }

    private void openShare() {
        Resources resources = getResources();

        Intent emailIntent = new Intent();
        emailIntent.setAction(Intent.ACTION_SEND);
        
        emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
        emailIntent.setType("message/rfc822");

        PackageManager pm = getPackageManager();
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("text/plain");


        Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));

        List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
        List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
        for (int i = 0; i < resInfo.size(); i++) {
            // Extract the label, append it, and repackage it in a LabeledIntent
            ResolveInfo ri = resInfo.get(i);
            String packageName = ri.activityInfo.packageName;
            if(packageName.contains("android.email")) {
                emailIntent.setPackage(packageName);
            } else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("text/plain");
                if(packageName.contains("twitter")) {
                    intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
                } else if(packageName.contains("facebook")) {
                    intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
                } else if(packageName.contains("mms")) {
                    intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
                } else if(packageName.contains("android.gm")) {
                    intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
                    intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
                    intent.setType("message/rfc822");
                }

                intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
            }
        }

        
        LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);

        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
        startActivity(openInChooser);
    }
&#13;
&#13;
&#13;

但我需要自定义BackgroundColor: - )