1

I have to share a url on android default but i want a response if url is being shared or just intent is closed. I am using on activity result but when i share with gmail it return 0(CANCELED) same when i close the intent.I need this to set text shared on a text view.Here is my code.

public void executeShareLinkClick() {
   Intent intentShare = new Intent(ACTION_SEND);
    intentShare.setType("text/plain");
    viewModel.isLinkSharedOpen.set(true);
    intentShare.putExtra(Intent.EXTRA_TEXT,"My Text of the message goes here ... write anything what you want");
    startActivityForResult(Intent.createChooser(intentShare, "Shared the text ..."),111);
}

public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
   if (resultCode==RESULT_OK){
       viewModel.isLinkShared.set(true);
   }else{
       viewModel.isLinkShared.set(false);
   }
    viewModel.isLinkSharedOpen.set(false);
}
Ergi16
  • 13
  • 2

2 Answers2

2

Sorry, but what you want is not an option. ACTION_SEND does not support a result, and apps do not have to indicate whether or not the user sent your content.

The closest thing you can do is to add EXTRA_CHOSEN_COMPONENT_INTENT_SENDER to the Intent returned by createChooser(). Through that, you can find out if the user chose something in that chooser. However:

  • I don't think you find out if the user chose nothing

  • It still does not indicate that the user actually used the chosen app to send your content

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • How to find out if user choosed something with this EXTRA_CHOSEN_COMPONENT_INTENT_SENDER? – Ergi16 Jul 16 '21 at 22:27
  • @Ergi16: You pass an `IntentSender` as an extra using `Intent.EXTRA_CHOSEN_COMPONENT_INTENT_SENDER` as the name of the extra. You can get an `IntentSender` from objects like a `PendingIntent`. It will let you indicate how you wish to receive the response. This approach is not widely used or documented, so you may encounter some problems. Also note that this approach is only available on Android 5.1 and higher, though fortunately that encompasses most devices today. – CommonsWare Jul 16 '21 at 22:32
-1
if(getIntent().getData() != null) {
    Uri fileUri = getIntent().getData();
    File file = new File(fileUri);

}

// for multiple files
if(getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE) && getIntent().getClipData() != null) {

    int count = getIntent().getClipData().getItemCount();
    int currentUri = 0;
    while(currentUri < count) {
        Uri fileUri = getIntent().getClipData().getItemAt(currentUri).getUri();
        File file = new File(fileUri);
        currentUri = currentUri + 1;
    }
}
TomInCode
  • 506
  • 5
  • 11