We have seen in Part 2 how to use Explicit Intents to invoke activities through a very simple example. Now, we will move on to a more interesting concept of Implicit Intents and Intent Filters.
This requires a little of theoretical understanding before we move on to an example.
As described earlier, an implicit intent does not name a target component that should act upon the intent. I
also said that the android platform resolves as to which component is best suited to respond to an Implicit Intent. How does this happen?
Basically, an Intent object has the following information (among other things like Component name, extras and flags) which is of interest for implicit intents:
- Action
- Category
- Data
So, the android platform compares these 3 (action, category and data) to something called "Intent Filters" that are declared by probable target components who are willing to accept Implicit Intent calls.
i.e. Intent Filters are the way of any component to advertise its own capabilities to the Android system. This is done declaratively in the AndroidManifest.xml file.
So here are some important points to remember:
- Implicit Intents do not specify a target component
- Components willing to receive implicit intents have to declare their ability to handle a specific intent by declaring intent filters
- A component can declare any number of Intent Filters
- There can be more than one component that declares the same Intent Filters and hence can respond to the same implicit intent. In that case the user is presented both the component options and he can choose which one he wants to continue with
- You can set priorities for the intent filters to ensure the order of responses.
There are 3 tests conducted in order to match an intent with intent filters:
- Action Test
- Category Test
- Data Test
For more details about them, you may visit the Android developer documentation here.
Finally we shall look at declaring an implicit intent in one activity which will invoke one of the native activities of the platform by matching the intent filters declared by the same.
The complete code for a very simple implicit intent example that has been described in this article is available for download here.
The InvokeImplicitIntent Activity creates an implicit intent object "contacts". This intent object's component is not set. However, the action is set to "android.content.intent.ACTION_VIEW" and the data's URI is set to "People.CONTENT_URI".
Such an intent matches with the intent filter declared by the view contacts native activity.
So, when you run this application, it displays the native UI for viewing the existing contacts on the phone!
Here is the relevant piece of code for the same:
Button viewContacts = (Button)findViewById(R.id.ViewContacts);
viewContacts.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);
startActivity(contacts);
}
});
In this manner many of the native applications can be seamlessly invoked as one of the activities in our applications through implicit intents.
---------------------------------------------------------------------------------------------------------
Updated on 31st March 2010:
The above example uses Android SDK 1.5.
From SDK 1.6 and above, the Contact.People class has been deprecated and we need to use the ContactsContract class. So the line in code
contacts.setData(People.CONTENT_URI);
has to be replaced by
contacts.setData(ContactsContract.Contacts.CONTENT_URI);
Here is the complete source code that has been tested with Android SDK 2.1
H Sai Geeta,
ReplyDeleteGood postings,Thanks for your tutorials. I have a question could you please make sure the following things in your sample
contacts.setData(People.CONTENT_URI);
what is people,content? How we will know this to be?
And
I want to get battery history,(spareparts/device info)what will be the action,and URI?
do we need to import any package..?
Thanks
Medha
dear, contacts.setData(People.CONTENT_URI), People is the by default keywork in ContentProvider class.
DeleteHi Medha,
ReplyDeleteAny activity that can be invoked by another activity through implicit intents has to tell the action to be done and the data on which the action needs to be done (at the minimum).
And the application / activity that has declared a intent filter that matches the action will respond to it.
In this case, I have used an inbuilt contact application for invoking though an implicit intent. So, I look at the API documentation for the contacts application and that gives me the action and data information.
Since I have said that the action is VIEW, I must also say what is the data on which this action has to be performed. Since it is the contacts data that I want to view, I got from the android.provider.contact.People class the data URI constant as CONTENT_URI
That is how I have set it here.
For viewing the other stuff you have mentioned, the action probably is same VIEW action. (note this should match the intents declared by the classes that provide this information). You explore the android.provider.Settings class and may be the Settings.System class as well.
thanks Geeta, I really appreciate your help. I never expected response, and that too with in a day.
ReplyDeletehi sai geetha,
ReplyDeleteI would like to know about how to call one application from other.
Say activity of one application calling the activity of other application.
see the example of ExplicitIntent, it will help you to learn how to call one applicatin from another.
DeleteHi,
ReplyDeleteyou can invoke an activity in another application through the concept of implicit and explicit intents. Most often it will be through explicit intents, esp if the "other" application that you want to invoke has declared that it can respond to certain "action" and "category" or "data" as explained in my explicit intent tutorial.
Hi Sai Geeta
ReplyDeleteI'm new to android and i have a question concerning implicit intent.
i'm developping and application and i want to know if it was possible to use the implicit intent to invoke an application downloads on android market. and how in this case have the informaion on the actions and data defined by this application
Hi ptolémé,
ReplyDeleteThe action and data declared by the downloaded application should be available in the application documentation or in the android manifest file.
Only if they intended to expose the activity within their application to be invoked by other applications implicitly, they would have provided this.
hi Sai Geeta
ReplyDeleteThank for your reply.
In fact i'm developping an application which will use a bluetooth communication to load data from an server, the problem is that i'can't find the library to do it programmatically. I know in other hands that some applications like AndFTP are avalaible in the android Market. Do you think that it could be possible to use the fonctionnalities of this application in order to develop my application? and if not do you have any ideas or development experience about bluetooth ?
Thank you again for everything you do for the developer community
Hi ptolémé,
ReplyDeleteI have not tried working on the bluetooth aspects of Android. So, I will not be able to help you on that.
However, a request to the visitors to my blog, if anyone can answer this, this can be come a good discussion forum.
Let it be mutual give and take :)
hello everyone,
ReplyDeleteI read the whole discussion, implicit intents can be used to call other applications and the native android apps also.... but there is a limitation....if an app activity has declared only ACTION_MAIN in its intent filter then how can we call that activity from our app.....
as I tried to call customlocale native app frm my app...
Is it possible?
If yes then How?
Hi Sai Geeta ..
ReplyDeleteThanks for your tutorials, very clear and to the point, this is a great resource.
It would be great if you could clarify one thing i didn't fully understand: you said in a reply to a comment above that "you can invoke an activity in another application through the concept of implicit and explicit intent" ...
From what i read i thought only explicit intents could load a different app (through the intent filters), and that implicit intent are to be used to call external applications ... Is this wrong .. ?
Thanks a lot x your help ..
Daniele
Hi Daniel,
ReplyDeleteI am not sure what was your question on explicit and implicit intents.
Here is what I understand: With explicit intents, you are explicitly invoking another activity or another android component by calling it in particular.
In implicit intent, you create and intent and expect any application / activity/ component which can respond to such an intent to pick up this call and get invoked. If there are more than 2 suc applications declaring the same intent, infact, Android will show both the applicaitons in response and the user can choose which one he wants to call.
hi sai,
ReplyDeleteUr tutorials are much helpful for quick learning of Android.
Can u plz provide any tutorial for httpconnections in android.?
HI SAI !
ReplyDeleteYour Android tutorials were very much interesting. Can u provide a tutorial describing how to make httpconnectins in Android.
I will be waiting for ur reply.
thanks.
Hi.. Nice article...
ReplyDeleteI'd just like to know if there is a way to change the look of the default application (Contacs) inorder to match my application that is invoking it??
-Wafa.
Hi,
ReplyDeleteI have make two applications where one of my application calls an activity of other application implicitly and using permissions.
I am not able to understand what action and category I should add in the intent filter of the called application. I have to send some data from my first application and show it on the second.Please help me .Right now I have made custom action and category but everytime it is saying no activity found to handle the intent.
this is my client application
package com.client.cust.perm;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
public class ClientCustPermMainActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button)findViewById(R.id.btn);
btn1.setOnClickListener(this);
}
public void onClick(View v)
{
Intent intent = new Intent();
intent.setAction("com.cust.perm.action_MyAction");
intent.addCategory("com.cust.perm.category_Mycategory");
//intent.setClassName("com.cust.perm", "com.cust.perm.PrivyActivity");
startActivity(intent);
}
}
and the manifest of the called application is as below
I have already given permission in my client app using uses-permission
Please tell me where am I wrong
thanks in advance :)
hey my manifest file did not come , I dun know why. But it has the custom category and action defined in my client application.
ReplyDeletehi geetha my name is masood
ReplyDeleteIn my application there is one button and four text fields.
how to write a code when user clicks the button the map should appear the remaining fields must go down below the map how to do this kind of application in android
hello geetha,
ReplyDeletemyslf is harish i m new in andriod programmng can u tell me how to make two text boxes having to button with label ok and cancel and when i enter any no in them they tell me result in log and in expotentional form in anther window.lz reply soon
Needed suggestions
ReplyDeleteI have created one register form.
but i want use intents to show the register form data in to another activity... Just i want display the form fields of data to another activity.....
This comment has been removed by the author.
ReplyDeletehi,
ReplyDeleteTHANKS FOR THE TUTORIALS. I AM WORKING WITH IMAGEZOOMVIEW FOR ZOOMING AN IMAGE.I HAVE INCLUDED OPTIONS MENU. I AM NOT ABLE TO CALL OTHER ACTIVITY FROM SUBMENU CLICK.. COULD YOU PLEASE HELP..
REGARDS
Respected madam,
ReplyDeleteI am Raghav Rajagopalan from Chennai. I am currently working on Android Application development. Only last week i got into this domain. I have a question to ask you. Could you kindly suggest me a complete reference book for android for beginners. I need to develop an Login page, Which i am done with. Onclick Register button on Login screen, i need to navigate from login screen to a website in case if i need to register in my web site (like (Intent)). I need a code for that. Since i need to make it by this week end can you help me out on this issue.
Make my requirement clear:
1.i have a login page with register button.
onclick register button must navigate to next screen which has a web site. where i need to register online.
2. Back button in second screen(Web site Screen) must navigate to login screen.
Could you also suggest me how to work on Web Service.
I have posted on many blogs but hardly found any replies. I hope you u suggest me the solution for this issue.
Thanks in advance.
Regards,
Raghav Rajagopalan
Hi,
ReplyDeleteIs it possible to change the font style of tab option's using setIndicatior.....
If so plz tell me....
Is there any other way......
Hi sai,
ReplyDeleteCan u give me the More explanation for Intents and intent filters........
Hai sai geetha....
ReplyDeleteThanks for the nice tutorial.But i have one doupt.For this application we can use default .If i want to call this application from another page, then what all are the changes i have to make in the section in the manifest file.I think i can use default for the first page and need to create new for the called activity.But i don't have any idea about what i can write in the new .Pls help me Madam.
Hi sai geetha
ReplyDeleteI am begginer in android application development.
R.java file is not being created in my project.I have tried solution like clean the project..eventhough it is not working properly.
kindly know the solution..
pls re install ur IDE settings.......
Deleteclean once again &
Deletecheck andriod manifest xml
Hai Sai Geetha,
ReplyDeleteIs it possible to invoke the custom activity through Implicit intent. Is it right approach to do as like it?....
Hi sai geetha
ReplyDeleteI am beginner android application developer.
R.java file is not being created in my project.I have tried solutions like clean the project,removing .jar file from java build code..eventhough it is not working properly.
kindly know the solution..
Hi Anonymous
ReplyDeleteRegarding R.java
Actually if anything wrong in your XML file it will effect in R.java file.
I have one solution if R.java is not exist in your application. create ID for any widget like textview or button etc in XML file. For example in main.xml do any changes or create ID for any textview or button
After that save XML file then R.java will come automatically.
Regards.
Ravinder Reddy
Hi,
ReplyDeleteabout R.java
Do any changes in XML file or else create ID for any widgets like textview or button then save it, then R.java will come.
Ptolémé,
ReplyDeleteHave you taken a look at the bluetooth chat example that automatically comes with some of the SDK samples?
Do "New Android Project..." > pick Android 2.2 > and select the radio button "Create new project from sample"
I do not know of any open intent for the application you've listed. Just do a google search for bluetooth and open intent to see what's out there.
If you can't find a open intent for what you need, please note that you should try to break down the problem into two smaller pieces. First, there is the bluetooth communication, and being able to pass text some text through that connection. And then, there is the problem of listing/navigating/operating on a directory structure (I assume a directory structure inside your SD card). First, solve those two problems separately, then try to combine your results.
Stephan
very usefull tutorial thanx a lot Sai Geetha di
ReplyDeletehi sai geetha,
ReplyDeletemy database contain values are(name, age, marks)from this i retried name and display in one listview ... then i clicked that name in another listview i want show the details of that name how i want do plz help me....
thanks
suresh
Hi SaiGetha
ReplyDeleteThanks For your Nice Tutorial...
Regards
Chandu
HI sai Geeta,
ReplyDeleteNice Tutorial...
Can you please prived an example to invoke an activity from other application.
Thanks,
Swetha.
its really useful....thanks a lot
ReplyDeleteThank geeta, I found this blog really helpful.
ReplyDeletehello Sai Geeta
ReplyDeletei am shakac from Bangladesh.
i have no sentence how to thank you.this tutorial is really awesome.
to gain android development knowledge i read some books include android cookbook but i did not got a clear idea about intent and activity,from this tutorial i gained a really clear idea about activity and intent and other component also.
long life Geeta
and many many many thanks to you again
Hi Sai geetha,
ReplyDeleteThis is Velu from coimbatore. I'm Newbie to android.
I used the following implicit intent to get some information from google map.
Intent searchAddress = new Intent(Intent.ACTION_VIEW, Uri .parse("geo:0,0?q=" + location)); startActivity(searchAddress);
It shows all the addresses but i want to get information about hospitals in india only. What should i do to filter that intent. is it possible to do so..?
Hai madam,
ReplyDeleteThis is chandra sekhar.I want your help in providing the example program based on intents in which when you click the login button it has to open the next activity but without entering the user name and password it dont goto next activity.Please help me.
Hii Sai Geetha Madam,
ReplyDeleteI think you are no more using your blog, as you had ranaway from the commitment you had made to userself while creating this blog that you will always help the android learners and will always be there to give and take the android doubts and queries...If this is true then please remember the famous saying...Today its me tommorrow its you...
I am A Dedicated fresher in Android.
Have you any idea what you are talking about? It's a person's choice what he/she wants to do. Sai Geetha did her best by putting up a post on a blog. A blog is a place where one says what he/she wants to share. It's NOT a support desk. And, most of the doubts above can be solved by reading the manual.
DeleteWhile you are reading the blogosphere, make sure you thank the writers for what they have given, or just keep your mouth shut because you couldn't do the same.
Sai Geetha, it's been a pleasure reading your tutorial.
For others, RTFM before you post here.
Hi Pravin,
ReplyDeleteI have made no such commitment when I started this blog. this is just a way of sharing what I have learnt. If you other want too, they too can share. But I am not way obligating myself to be readily sitting and answering queries at all... Hobbies are always done when you find the time for it.
Its open source nature makes it simple for the Android development company to generate mobile applications at affordable prices. The Android Software Development Kit (SDK) is helping for the Android app developer.
ReplyDeleteHi Sai Geetha,
ReplyDeleteThis is Mani from Delhi. I'm Newbie to android. i just wanna know that how we call any application(created by a user not system application) is called from another application (not system application)via implicit intent...i hope you understand my query .
Hi Sai Geetha,
ReplyDeleteThanks a lot for the tutorial, I had one issue with file editing in android using implicit intents, it got solved know, It took lot of time and permutations and combinations to solve it, but still finally i am left with doubt, The problem got solved but my quest to know why the problem got solved is not solved.
Please let me know if in case u have any clue on this.
Ok coming to my problem.
1. I have an activity. I have a Button in the activity. I want to open a pre existing log file (which is a text file example log.txt) stored in the location "/mnt/sdcard/xxx/log.txt"
2. The below is the implicit intent code i wrote and i ended up with an exception " No activity found"
code1: which i tried and got exception
Uri uri = Uri.parse("file:///sdcard/xxx/log.txt");
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_EDIT,uri);
viewTestLogFileIntent.setType("text/plain");
code2: which i tried and got exception
Uri uri = Uri.parse("file:///sdcard/xxx/log.txt");
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_EDIT);
viewTestLogFileIntent.setData(uri);
viewTestLogFileIntent.setType("text/plain");
code3: which i tried and working fine
Uri uri = Uri.parse("file:///sdcard/xxx/log.txt");
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_EDIT);
viewTestLogFileIntent.setDataAndType(uri,"text/plain");
Two doubts i have are
1. First of all my file is located in /mnt/sdcard/xxx/log.txt this i can clearly see in the file system in DDMS view of eclipse, but how is it working when i give the file link in uri as "file:///sdcard/xxx/log.txt" where i skipped /mnt from path
2. what is wrong with code1 and code2?
what ever is the data and type i am setting in code3 i am setting same data and type on intent but with different methods like setData() and setType() seperately. why are they ( code2 & code1) not working? why is the code3 working?
Just for the information, the codes which i tried and which are working fine are
DeletecodeA:
Uri uri = Uri.parse("file:///mnt/sdcard/xxx/log.txt");
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_VIEW);
viewTestLogFileIntent.setDataAndType(uri,"text/plain");
startActivity(viewTestLogFileIntent);
codeB:
Uri uri = Uri.parse("file:///sdcard/xxx/log.txt");
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_VIEW);
viewTestLogFileIntent.setDataAndType(uri,"text/plain");
startActivity(viewTestLogFileIntent);
codeC:
File TestLogFile = new File("/mnt/sdcard/xxx/log.txt");
Uri uri = Uri.fromFile(TestLogFile);
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_VIEW);
viewTestLogFileIntent.setDataAndType(uri,"text/plain");
startActivity(viewTestLogFileIntent);
codeD:
File TestLogFile = new File("/sdcard/xxx/log.txt");
Uri uri = Uri.fromFile(TestLogFile);
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_VIEW);
viewTestLogFileIntent.setDataAndType(uri,"text/plain");
startActivity(viewTestLogFileIntent);
In My android device i have polaris office, DropBox Text editor (DB text editor) using any of the above codes when a make a click on the button i am getting a gracefull pop up asking me for viewing the text file with two options "DB Text Editor" and "Polaris Office".
In all the above codes if i set the data and type seperately using viewTestLogFileIntent.setData(uri);
viewTestLogFileIntent.setType("text/plain")
or
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_VIEW,uri);
viewTestLogFileIntent.setType("text/plain")
I am ending up in a same big fatal exception "No Activity Found" or i am ending up without any PopUp for viewing the text file.
Dont know the reason why?
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteHi madam,
ReplyDeleteThank you very much for your examples... Very useful.... I like to play videos from my sd card using intent. Can you please explain me how it will be possible.. I tried a lot... Thanks in advance...
Hi,
ReplyDeleteExample u have explained works fine, But can u explain intent filters using XML files??
Hi Sai Geetha,
ReplyDeleteThanks a lot for sharing your knowledge Its v to the point and easy to understand