I am creating an application where I need to call another application that is already installed in the device on button click.
I have done some research on it and I understand that I will need to call an intent for the same. What I dont understand is I do not have a class name for the application I want to call. For example, if I want to call the device's gallery from my application on button click, how do I do that?
Uri uri = Uri.parse("file:///sdcard/");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setType("application/pdf");
startActivity(intent);
return true;
Thanks guys. I have tried this code but it said document could not be opened.
You will need to call Implicit Intents
From the documentation:
Implicit Intents have not specified a component; instead, they must
include enough information for the system to determine which of the
available components is best to run for that intent.
These intents can be triggered providing any action, type or category information
For example you want to open browser Activity and you don't know the Activity class name you will use something like this:
Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(in);
Another example: you don't know the Gallery Activity class, you will call it using Implicit Intent like this:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
Here the call
Enjoy coding....
Intent res = new Intent();
String mPackage = "com.ReachOut";
String mClass = ".splash1";
res.setComponent(new ComponentName(mPackage,mPackage+mClass));
startActivity(res);
If the exact application is less important, and you just need something that will display your content, you can omit the component name entirely, too. Just set the action, data, and (optionally) type on the intent and let the OS do the work.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(/*your data uri*/);
intent.setType(/*your data's MIME type*/);
startActivity(intent);
This intent will be handled by some app that has registered for intents with the view action and the appropriate MIME type. This is an implicit intent, like Adil mentioned.
Related
I am using the code to call an intent in Android
Intent intent = new Intent();
String PACKAGE_NAME="com...."
intent.setPackage(PACKAGE_NAME);
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
getApplication().startActivity(intent);
Unfortunately, in some case, I do not know the PACKAGE_NAME. So, another method is using broadcast. How can I use it? Thanks all
You cant call Activity directly if you dont know package and name of that Activity.
Since you dont know package and Activity name you can only ask OS to show all possible variants for your Intent Action. In most cases user click "always open this app for this action" what means that you will directly open another app.
So in your case your code should looks like
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//dont forget to check if user has at least one application for your Intent Action
if (intent.resolveActivity(getPackageManager()) != null) {
context.startActivity(intent);
}
I have read documentation of setType but arguments are not mentioned. Where can I read full possible string arguments that use inside setType() keyword. I need information on setType("vnd.android-dir/mms-sms"). I am working on some messaging app and this setType works fine with me. It takes user to default messaging app. What I want is open write message box in default messaging app. I use code like below, but it only takes me to inbox.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms/"+phone_number_in_string);
startActivity(intent);
What I want is open write message box in default messaging app.
Use putExtra:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms/");
intent.putExtra("address", phone_number_in_string);
intent.putExtra("sms_body", " ");
startActivity(intent);
I want to update an apk through code. However I need to establish a mechanism that checks when that application has updated. I need to somehow get a message back from the intent
Context ctx = getApplicationCOntext();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/Download/update.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
This is the default mechanism to install an application. How can I check that if the intent has finished?
Use startActivityForResult() if you want data to be return from the called activity. For more details, read: Getting a Result from an Activity How to manage startActivityForResult on Android?
I need to get the path of the result image path from intent without using the onActivityResult.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
I need the path of the image to the next step in OnCreate which is after the this intent started. Is there any way to get this done without using OnActivityResult ??
I need the path of the image to the next step in OnCreate which is after the this intent started
That is not possible. startActivityForResult() does not even begin doing its work until after your current callback (onCreate(), apparently) returns.
Is there any way to get this done without using OnActivityResult ?
No.
I need the result image path to pass it to a new method and start a new intent.
Do that in onActivityResult().
this intent can be only created inside the onCreate due to my application structure
That seems unlikely. Even if for some bizarre reason it is true, create the base Intent in onCreate(), store it in a field of your activity, and use the Intent in onActivityResult().
I want to launch some apps which accept geo coordinates (http://developer.android.com/training/sharing/send.html). The problem is that they want different URIs.
Example (pseudo code):
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.addSomeUri("http://maps.google.com/maps/?daddr="+myAddress);
intent.addSomeUri("http://someotherservice.com/?coordinates="+myLat+":"+myLng+"&address=myAddress");
EDIT:
Of course the goal is for both apps to appear in the same activity chooser.
How can I achieve it?
How can I achieve it?
Call startActivity() (or startService() or whatever) once per Uri.
Do you really want to launch every app?
If not, you have simpler way
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:30.0, 120.0"));
startActivity(intent);