Share multiple posts with the same intent choice in my app

I have this code

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

And I want to share another message containing text and image in the same application of choice.

Do you have an idea on how to solve the problem?

+4
source share
1 answer

You can use ShareActionProvider. In the following example, I use one associated with MenuItemin Toolbar, but you can also use them, for example. a Button.

main_menu.xml

<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>

activity_main.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>

MainActivity.java

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

Remember to make sure that sendIntent.resolveActivity() != nullif you use it much later - another application may have been uninstalled during this time!

0
source

Source: https://habr.com/ru/post/1687195/


All Articles