How to get callback of ACION_SEND Intent - android

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.

Related

Wrong application provided to open in the Intent chooser when I need only map applications

I've got several problems.
I have a task to provide different map applications to a user when he pushes a button.
I do it by creating an Intent.createChooser. Everything works fine until the user install application "Zoom" for online meeting. And it's literally not what user expects to see in the dialog of apps and obviously he can't use it as map application.
I create an Uri with coordinates, put it to the Intent with ACTION_VIEW. Like this:
val mapIntent = Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=53.9000,27.5667"))
Then i create Intent chooser:
val chooser = Intent.createChooser(mapIntent, "Build a route with:")
And then I start Activity with chooser: startActivity(chooser)
And then I get a wrong app "Zoom" in this dialog:
WRONG DIALOG SCREENSHOT
The first question is:
Maybe there are some ways to see how actually Zoom App appears in this dialog and why my Intent receives this app activity?
The second problem is a case how I try to solve the first one. So I tried to use queryIntentActivities for my Map Intent and see in Log what activities are suitable for my Intent. It should return me a list of all activities that Intent chooser shows me, but in fact it returns me only one! activity MapsActivity:
val pm: PackageManager = packageManager
val activityList = pm.queryIntentActivities(mapIntent, MATCH_ALL) //or MATCH_BY_DEFAULT, no difference
activityList.forEach {
Log.d("ACTIVITY MAP", it.toString())
}
And in Log it shows next info:
D/ACTIVITY MAP: ResolveInfo{6c40733com.google.android.apps.maps/com.google.android.maps.MapsActivity m=0x208000}
Only one MapsActivity is shown... Where is the others Activities that shown to me in the Intent chooser?

Android share text always ask for application

I use this code to share text from my application:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, GetTxt());
intent.setType("text/plain");
startActivityForResult(intent, REQUEST_SEND_MSG);
and it appear the activity where I can choose the application on which I want to share.
I noticed if I click on "Always open with..." I can't change it.
So how do I force the user to always select for application?
On api 29 "PackageManager.clearPackagePreferredActivities" is deprecated so what can I use?
You can try with an intent chooser like this:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, GetTxt());
intent.setType("text/plain");
startActivityForResult(intent, REQUEST_SEND_MSG);
Intent openInChooser = Intent.createChooser(intent, "Share");
startActivityForResult(openInChooser, REQUEST_SEND_MSG);
It's generic, so you can share your text with what you want
Your app cannot clear the preferred activities from other apps that are set by the user as default activities. Up until API 28 you could use clearPackagePreferredActivities to unset the default activities if they belonged to your app, but now with API 29 this is deprecated.
This method was deprecated in API level 29.
This function no longer does anything. It is the platform's responsibility to assign preferred activities and this cannot be modified directly. To determine the activities resolved by the platform, use resolveActivity(Intent, int) or queryIntentActivities(Intent, int).
Instead, it's suggested your app queries for role availability with Role Manager.
To configure an app to be responsible for a particular role and to check current role holders, see RoleManager.
That said, there is also this answer with a hack method of achieving that, but I have no clue if it still works or not. Also, I imagine if it works, it would only work the first time.

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

Android photoviwer VIEW intent only showing 1 image

In my android app, I create an intent to display an image in the photogallery app.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///storage/sdcard0/arin/category/1/1.png"), "image/*");
context.startActivity(intent);
This opens the photo app and shows that image but only that image...
I know there are other images in the same folder. But the app wont let swipe left/right and switch images. How can I change the code so it will let me see the other pics in the same folder...
Thanks
How can I change the code so it will let me see the other pics in the same folder
Write your own "photogallery" code.
There are hundreds, perhaps thousands, of apps that are capable of responding to your Intent. None of them have to offer any means to "swipe left/right and switch images". If you want a specific set of behaviors, write them yourself. If you want the user to be able to view the image in the user's chosen app, then stick with your existing code and do not worry about the behavior of the user's chosen app.

Detecting the target application when sending an Intent

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

Categories

Resources