You can use ShareActionProvider
. In the following example, I use one associated with MenuItem
in 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;
}
}
private void setShareIntent(Intent shareIntent)
{
if (shareActionProvider != null)
{
shareActionProvider.setShareIntent(shareIntent);
}
}
}
Remember to make sure that sendIntent.resolveActivity() != null
if you use it much later - another application may have been uninstalled during this time!
source
share