I'm trying to open a pdf file from my app , on my device currently installed 3 3rd party apps to open pdf file. when the system asks me on witch of them to open the pdf, how can i know if the user selects one and presses "ok" or "decline"? my actions defined by if he accepts or declines
This is in my class :
String path="...../file.pdf";
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "application/pdf");
startActivityForResult(intent, 225);
And then in the activity :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 225) {
// Make sure the request was successful if (resultCode == RESULT_OK)
} else {
}
}
screen shot for example
You can find all of the apps on the device that can service your Intent using the following code:
List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
You can use the information from this to query the packages and learn their display name and icons (SO has plenty of answers how to do that).
You can create your own Dialog from within your app that looks identical to the system dialog. In this way you'll be able to track hits and pass the intent to the selected package directly.
Good luck!
how can i know if the user selects one and presses "ok" or "decline"?
You can't. ACTION_VIEW that is used to open a pdf in an external app does not give back any information. You can use startActivityForResult() but it will not have any effect because a result will not be set. Also see ACTION_VIEW documentation.
You should redesign your concept/logic so that it does not depend on this information.
Related
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.
I am looking for a light easy way, to pick an image from a gallery, and pass to another intent/activity the image selected.
This is what I have but I know it is wrong, I was just looking for some quick insight.
I know this opens up an image to be selected, but once it's selected, nothing happens, it does not load my intent. I am aware, from my belief once the image is selected it is just not dong anything and is returning true or w.e. when chosen and not providing anything, to which I ask if I need some more methods to be called that I haven't included? thank you.
Code:
item.getTitle().equals("Upload")) {
// TODO might want to pass parameter of what fragment is loaded.
// Switch to upload activity to allow for uploading of images.
Intent uploadIntent = new Intent(this, UploadActivity.class);
uploadIntent.setType("image/*");
uploadIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(uploadIntent, "Select Picture"),
PICK_IMAGE);
//startActivity(uploadIntent);
}
I'm not sure I fully understand your requirements but if you're ok with launching the default system image gallery, then the following post may help you:
How to pick an image from gallery (SD Card) for my app?
The general idea is to request a new Activity handle an Intent requesting a result (startActivityForResult()). If an Activity is setup to receive the intent (ACTION_GET_CONTENT) then it will launch and handle your request. Once the request is complete, the Activity will return a result which your Activity will recieve in it's method
onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent);
I'd like to set up something to launch the built-in contact picker, and let a user choose an email, phone number, etc, then get said address/phone number back in my activity.
Am I correct in assuming that this isn't possible with android? It seems like you have to at least build a dialog yourself to choose in the case where a contact has multiple phone numbers/emails.
Even before that though, there doesn't seem to be a way to choose both phone numbers and emails simultaneously.
Is something like https://github.com/codinguser/android_contact_picker or rolling your own UI the only way to go?
Its perfectly possible and straight forward with android. You don't need to create any dialog box or anything for it. Just lauch the built in contact picker Activity via an Intent.
It you have to launch the intent from some event handler
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
You should also have to implement
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
to receive URI of the contact user picked.
Thousands of tutorials are available on INTERNET on how to do it. Google is your friend.
Here is one http://mobile.tutsplus.com/tutorials/android/android-essentials-using-the-contact-picker/
What I would like to do is the following:
User selects a shortcut from a list of all available shortcuts in the system;
The relevant info is stored;
User performs an action and the selected shortcut is executed, like if it was an icon on the home screen.
So far I am able to populate and present a list with all the shortcuts, using
getPackageManager().queryIntentActivities(new Intent(Intent.ACTION_CREATE_SHORTCUT), 0);. Upon selecting a shortcut, I start the ACTION_CREATE_SHORTCUT intent to customize the shortcut parameters - it presents the proper UI and seems to work. I use this code to start the intent:
ActivityInfo activity = resolveInfo.activityInfo;
ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
Intent i = new Intent(Intent.ACTION_CREATE_SHORTCUT);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivityForResult(i, 1);
Here is my onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==1 && resultCode == RESULT_OK) {
try {
startActivity((Intent) data.getExtras().get(Intent.EXTRA_SHORTCUT_INTENT));
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Success!!", Toast.LENGTH_LONG).show();
finish();
}else{
Toast.makeText(getApplicationContext(), "Fail: "+resultCode+" "+resultCode, Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
Now, the problem is that the onActivityResult always gets triggered immediately after startActivityForResult with requestCode=0, resultCode=0 and with no data. It does not trigger when the ACTION_CREATE_SHORTCUT activity actually ends. I really don't get it. I think that after the activity ends it should return the requestCode I sent it and the data intent, containing the Intent.EXTRA_SHORTCUT_INTENT which I could then use somehow to actually start the shortcut.
The second part of the question is how do I actually store the necessary information for the shortcut the user selected, preferably in SharedPreferences, so I could later execute this shortcut with the specific parameters. I couldn't find any example of this.
Any help would be much appreciated! Thanks!
More then 2 years later, here is the answer to my question:
The proper functioning of the startActivityForResult/onActivityResult system obviously depends on both the calling and the called Activities being part of the same Task. Therefore any action which would cause the two activities to be launched in separate Tasks would break this functionality. Such actions include setting any exclusive launchMode for any of the Activities in the AndroidManifest.xml or using flags such as Intent.FLAG_ACTIVITY_NEW_TASK when launching any of the two Activities.
Upvoted the answer of user2427931 for the Intent.parseUri() solution.
I had the same behavior when my calling activity had launchMode="singleInstance". Do you have that defined as well?
Regarding saving the Intents. You could turn the intent into an URI and save it as a string in SharedPreferences. You could then use Intent.pareseUri() once you have retrieved it.
//Erty
Assuming I know that the Gallery has an album with a certain name X, what intent or broadcast can I make to open Album X with the Gallery app?
There are plenty of examples showing how to select a picture from the Gallery, but I don't see any describing how to accomplish the above.
Thanks.
try this
Intent intent = new Intent();
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Main.this.startActivity(intent);
program will browser all application are support with image file
but user can set default program
If you prepared the Gallery which is called X, yes you can do it.
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.Xapp");
if (launchIntent != null) {
launchIntent.setData("PHOTO ID OR ANYTHING ELSE");
startActivity(launchIntent);
}
After then you can set data in intent. And X app parses intent data and you can open it.
Otherwise, you can't this action. You only call the app, If the application does not provide API support for send data with intent.