Detecting the target application when sending an Intent - android

I am sharing image by using the send intent(ACTION_SEND).
I want to know if any application is selected for sharing or not. How can I do that and how do I know if the image was sent successfully?
Code I have used to share image is here :
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(imageSharePath)));
startActivity(Intent.createChooser(share, "Share Image"));

You need to implement your own dialog for the activity selection.
To create such dialogs you need to use PackageManager.queryIntentActivities(). This method returns List<ResolveInfo>.
ResolveInfo contains some information about an activity (e.g. resolveInfo.activityInfo.packageName), and with the help of PackageManager you can get other information (useful for displaying the activity in the dialog) - application icon drawable, application label, etc.
Display the results in a list in a dialog (or in an activity styled as a dialog). When an item is clicked create new Intent.ACTION_SEND, add the content you want and add the package of the selected activity intent.setPackage(pkgName).

The answer above is no longer correct.
Since API 22 it is possible to detect the target application when the user shares.
For details see:
Get IntentSender object for createChooser method in Android
https://medium.com/code-with-lisa/get-results-from-android-chooser-9cfc5445a871

Related

Prevent auto choose which sharing application use

I'm develop an application in Android.
I have a ListView with some image, when I click a row I open ContextMenu and I have two Choise, eliminate the image or sharing with other application.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
Uri r = Util.getImageUri(getActivity().getApplication(), image);
intent.putExtra(Intent.EXTRA_STREAM, r);
startActivity(intent);
I use this code to sharing the image, but after the first time, every time that I try to share image, the application choose always the first application with wich I send the first image.
How can I prevent it?
I want that user choose every time sharing application.
Thanks.
t
I use this code to sharing the image, but after the first time, every time that I try to share image, the application choose always the first application with wich I send the first image.
That was because you indicated, to the chooser, that you wanted to make this choice "always", instead of "just once".
I want that user choose every time sharing application
Use:
startActivity(Intent.createChooser(intent, "..."));
where "..." is your explanation for this.
In your phone go to settings-->Apps then select the app which get opens by default then there you will find an option "Open by default" open that option, in there press "Clear Defaults".
Then in your app again you will get the list of application to share with

How to show sharing menus within activity?

I want to share a text using this:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
As you can see in the below picture, I know how to acheive a dialog which lets user picks his choice (My app). But recently I saw an app which is letting users pick their choice within its activity (Desired).
Any idea how to acheive desired one? Thank you all.
This is the code, I've been using for sharing 'Referral Code' from within my app:
ShareCompat.IntentBuilder
.from(this) // getActivity() or activity field if within Fragment
.setText("message") // This will be populated as user's message
.setType("text/plain") // most general text sharing MIME type
.setChooserTitle("Share code using:")
.startChooser();
It opens up a view similar to your right image.
Create Custom layout. (Right image)
Embed in BottomSheetLayout
Use setState() to show your "Share Screen"
................................................
or You can just do the same inside an AlertDialog. (Left Image)
You can query for the list of apps that are registered to handle the action and then display the result in the way that you like the most. When the user will choose something you can start the selected activity.

How to get callback of ACION_SEND Intent

I tried this
private void postImage(Uri uri) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra(Intent.EXTRA_TEXT, "My bracelet image");
intent.putExtra(Intent.EXTRA_TITLE, "Action Bracelet");
intent.putExtra(Intent.EXTRA_STREAM,uri);
Intent chooser=Intent.createChooser(intent,"Share Image Using");
try{
context.startActivity(chooser);
}
catch(ActivityNotFoundException e){
Toast.makeText(context,"You don't have any share application installed",Toast.LENGTH_SHORT).show();
Log.e("Image Load","failed");
}
}
Now my problem is i need the application name on which this image is shared .I also created my custom dialog for it but problem remains same . Because when i select an option for share like facebook and i pressed back button then image is not share and i only know that the user click on facebook.
so i need a callback which gives me result_ok and result_cancle and the application name too . Can anyone help me i am stuck here from last three days ...
Now my problem is i need the application name on which this image is shared
If your minSdkVersion is 22 or higher, use the createChooser() that takes an IntentSender as the third parameter, as that is your only means of finding out what the user chose.
If your minSdkVersion is below 22, you will have to create your own chooser-style UI, using PackageManager and queryIntentActivities() to find out what activities should be listed in that UI.
I also created my custom dialog for it but problem remains same
You certainly know what the user chose in the dialog. That is all you are going to get from the API Level 22 createChooser() either.
Because when i select an option for share like facebook and i pressed back button then image is not share and i only know that the user click on facebook.
Of course. The user can do whatever the user wants in that other application. The user does not have to press BACK; the user can simply fail to send anything. That is between the user and that application — information about whether the user did anything, who the user shared the information with, and so on, is not available to you.

In share option in dialog box, I want to add my custom intent option

I am using this code to share link in android app.
Intent i=new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject test");
i.putExtra(android.content.Intent.EXTRA_TEXT, "extra text that you want to put");
startActivity(Intent.createChooser(i,"Share via"));
But i want to add cutom intent option in dialoag box.
If i have twitter and facebook app in my phone, then above code give me share option
1) Twitter
2) Facebook
3) Bluetooth(if phn supported)
Also with this I want to add
4) Commnets (on comments click user will go my Comment activity)
I want to make share like this.
Can anyone help. Thanks in advance.
You are creating a chooser. It contains all intents that reply to ACTION_SEND text/plain.
To have your comment activity in that list, you need make an intent-filter in your manifest matching that actions for your comment Activity.
Alternatively (and moreeasily, too), if you don't want your Comment activity to respond to external ACTION_SEND text/plain, you can use the response given to this question : Custom filtering of intent chooser based on installed Android package name : Add a Intent.EXTRA_INITIAL_INTENTS extra to the intent.

What is createChooser when I have to use Intents? What I could do with this method?

I have been taking a look over stackoverflow but I did't find a definition about what is "createChooser" and why I can use and in whick kind of situations is good to use it.
Thanks in advance.
For example: you have a share picture option in your application.
You define an intent like this:
Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);
picMessageIntent.setType("image/jpeg");
File downloadedPic = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS),
"q.jpeg");
picMessageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(downloadedPic));
Than when you call:
startActivity(picMessageIntent);
all applications on your phone capable of getting this picture will be listed.
If you want to custimize the title of that list, you can use createChooser like this:
startActivity(Intent.createChooser(picMessageIntent, "Send your picture using:"));
When startActivity(intent) is called for the first time from your app the user sees a list of all apps capable to handle this intent.
There is also an option to always handle this intent using one of the apps from the list. If this option is used then the list will never be shown again.
If you use createChooser in your intent then the "always use this app" option is not shown. The user always sees this list.
This method is used when you want to create a Custom Action using an Intent... Just like what android provides ACTION_VIEW etc... but here when there are multiple choices to perform an an Action this chooser will bring up a dialog which will have all available options and let the user select one... here is an example

Categories

Resources