Java – share multiple messages using the same intent selector in my application
•
Java
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);
I want to share another message containing text and images with the same selector application
Do you have any ideas on how to solve the problem?
Solution
You can use shareactionprovider In the following example, I use a MenuItem bound to a toolbar, but I can also use them with, for example, a toolbar 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. In 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 sendintent resolveActivity()!= Null if you use it later – other applications may have been uninstalled during this period!
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码