在我的应用中使用相同的意图选择器共享多条消息

时间:2017-10-10 08:12:36

标签: java android android-intent

我有这段代码

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) 
   sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
else  
   sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);

String activityTitle = cellsList.get(position).getTitle(); 
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, activityTitle); 
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);                                                               
sharingIntent.setType("image/*");
sharingIntent.setPackage("com.whatsapp");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Intent chooser = Intent.createChooser(sharingIntent,getResources().getString(R.string.share_using));
getActivity().startActivity(chooser);

我想与同一个选择器应用分享另一条包含文字和图片的信息。

您对如何解决问题有任何想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用ShareActionProvider。在下面的示例中,我使用了与MenuItem中的Toolbar绑定的示例,但也可以将其与Button一起使用。 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_item_share" app:showAsAction="ifRoom" android:title="Share" app:actionProviderClass= "android.support.v7.widget.ShareActionProvider" /> </menu>

main_menu.xml

<FrameLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sharing.datasender.MainActivity">

    <TextView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center_vertical"
        android:text="Send data once more"
        />

</FrameLayout>

activity_main.xml中

public class MainActivity extends AppCompatActivity
{
    private ShareActionProvider shareActionProvider;
    private Intent shareIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view = findViewById(R.id.view2);
        view.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                sendDataOnceMore();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        MenuItem item = menu.findItem(R.id.menu_item_share);
        shareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(item);
        setShareIntent(getShareIntent());
        shareActionProvider.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener()
        {
            @Override
            public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent)
            {
                ComponentName component = intent.getComponent();
                shareIntent = intent;
                String className = null;
                if (component != null)
                {
                    className = component.getClassName();
                }
                Log.d("TEST", "onShareTargetSelected: *" + className + "*");
                return false;
            }
        });
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();
        switch (id)
        {
            case R.id.menu_item_share:
                Intent intent = getShareIntent();
                setShareIntent(intent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private Intent getShareIntent()
    {
        Intent sharingIntent = new Intent();
        sharingIntent.setAction(Intent.ACTION_SEND);
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "This is my text to send.");
        Uri uri = Uri.parse(IMAGE_PATH);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
        sharingIntent.setType("image/*");
        sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        sharingIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        return sharingIntent;
    }

    private void sendDataOnceMore()
    {
        if (shareIntent != null)
        {
            Intent sendIntent = new Intent();
            try
            {
                sendIntent.fillIn(shareIntent, Intent.FILL_IN_COMPONENT + Intent.FILL_IN_ACTION + Intent.FILL_IN_CATEGORIES + Intent.FILL_IN_PACKAGE);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "And another thing...");
                sendIntent.setType("text/plain");

                sendIntent.setAction(Intent.ACTION_SEND);
                startActivity(sendIntent);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                Toast.makeText(this, "Boooom!", Toast.LENGTH_LONG).show();
                return;
            }
        }
        else
        {
            Toast.makeText(this, "No Go :(", Toast.LENGTH_LONG).show();
            return;
        }

    }

    // Call in onCreateOptionsMenu() or the MenuItem will not be clickable!
    // Call later to update the share Intent
    private void setShareIntent(Intent shareIntent)
    {
        if (shareActionProvider != null)
        {
            shareActionProvider.setShareIntent(shareIntent);
        }
    }
}

MainActivity.java

sendIntent.resolveActivity() != null

请记住确保max-width:none如果你以后使用这个版本很多 - 其他应用可能在此期间已经卸载了!