There are many applications that can pre-packaged into the android platform that can be reused by custom built applications because of the power of the android design that is based on implicit intents.(See part 3 of this series for implicit intents).
Here we will explore how to invoke the pre-packaged applications from our own through implicit intents. Note that in none of the snippets below, we actually call the pre-packaged or system applications. We just declare intents and pass them to an activity while starting the activity through startActivity() method. However, for each of these intents, the android platform finds the most befitting activity and invokes the same:
Here we will explore how to invoke the pre-packaged applications from our own through implicit intents. Note that in none of the snippets below, we actually call the pre-packaged or system applications. We just declare intents and pass them to an activity while starting the activity through startActivity() method. However, for each of these intents, the android platform finds the most befitting activity and invokes the same:
1. Call a number
Intent callNumber = new Intent();
callNumber.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:9440012345"));
startActivity(callNumber);
This will call the number 9440012345. For calling custom numbers, you could provide the user with a edit text field from which you can access the number and set it to the data above instead of a hard-coded number.
2. Browse the web for a given url:
Intent searchGivenText = new Intent(Intent.ACTION_WEB_SEARCH);
searchGivenText.putExtra(SearchManager.QUERY, "Android Examples);
startActivity(searchGivenText);
This will invoke the Google search engine to search the string "Android Examples" and return the results to you. This too can be generalized to accept a string from the user and set to the intent before starting the activity.
3. View google maps for a given location
Intent searchAddress = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=Bangalore"));
startActivity(searchAddress);
This shows the location of Bangalore on Google Maps.
4. View Contacts on the phone
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);
startActivity(contacts);
I have created an Android eclipse project which showcases all of these examples by taking inputs from the end user. You can access the same here.
--------Updated on March 31 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
Maybe instead "native" better
ReplyDelete"android system applications" ?
If I see "native" I think about application that use NDK, or use JNI...
Thanks.
Hi igor,
ReplyDeleteThanks for your comment. Yes, indeed it can mean different. So, I have changed the name to pre-packaged applications.
I suppose this should convey the message better
2. Browse the web for a given url:
DeleteIntent searchGivenText = new Intent(Intent.ACTION_WEB_SEARCH);
searchGivenText.putExtra(SearchManager.QUERY, "Android Examples);
startActivity(searchGivenText);
can we use specify any other search engine there other than google
for example www.vizconn.com
Hi Sai Geetha,
ReplyDeletethanks once again for making this tutorial available. It's been most helpful. Please I have a question which is not directly related to this tutorial but I though you may be able to help.
Do you know how I can display a custom view within an Activity? To be specific, I have a class that draws shapes on a Canvas and I am trying to display these shapes within my main activity which has buttons text views etc. I can see my custom shapes when I use setContentView(myCustomShapes) but this means nothing in my main view will be displayed. If I make a call to setContentView(R.layout.main) after setContentView(myCustomShapes) the main view obscures my custom shapes. Can you please help? I spent all night trying to figure it out without luck...:-(.
Cheers, Osagie
Hi Osagie,
ReplyDeletehave you created a custom view that can be declared in the xml. you need to create your own view that should also implement contructors that help inflate the view from the xml
public customview(Context ctx, AtrributeSet attrs, Map params) {...}
This will help you to declare your custom view too as part of the main.xml
You might have already seen this tutorial at anddev. http://www.anddev.org/creating_custom_views_-_the_togglebutton-t310.html
Hope that helps to begin with. This way, if you start with extending any basic view you will get the support of the view classes, to display both yours and default views together.
Thanks a lot Sai Geetha,
ReplyDeletethat was just what I needed. I have now been able to include my custom view in main.xml.
Cheers
thanks for this good tutorial
ReplyDelete:)
hi Nice articles.
ReplyDeleteCould you please help me how to launch a 3rd party App (Not pre packaged).
Say i have 2 applications, App1 and App2. Each in different packages.
I need to launch App2 on certain event of App1(say a button click)
I am unable to figure out.
Any pointers??
Regards,
Kushal
Hi Kushal,
ReplyDeleteThis is the fundamental aspect supported by Android. You can invoke any part of one application from another application through the concept of intents.
I hope you are clear about "intents" both implicit and explicit.
If both app1 and app2 are your own, you can make an explicit intent call from one to another to invoke the specific acivity that you want to call.
There is not single "main" method in one application to be invoked by another.
Hi sai,
ReplyDeleteIm adding the below ur code in my app.Its running time its shows the force close error.i adding the lib for interner in my xml also.please send why the error is coming?
Intent searchAddress = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=Bangalore"));
startActivity(searchAddress);
Hi Lakmag,
ReplyDeleteHave you chose the correct target when you created your android project?
you need to select it as "Google APIs".
In an already created project, if you open the default.properties file, if you are seeing
"target=Google Inc.:Google APIs:7"
then, taget is right. If you are seeing
"target=android-7" then it will not work. Let me know if this helps.
Hi sai geetha,
ReplyDeleteThanks a lot.Its working fine.very good articles.This article is very helpful to all android programmer.Did u having any blackberry article means please send the website link.
Thanks a lot for this tutorial
ReplyDeleteHi,
ReplyDeletehave you come across ActivityNotFoundException - particularly on Samsung phones.
I think maybe this is not a something we can rely on being on all Android phones.
Pete
Hi,
ReplyDeleteI found out that the to be browsed web page url should start with "http://", otherwise a ActivityNotFoundException will be thrown.
Regards,
Kwok
Hi,
ReplyDeleteForgotten to mention:
The try-catch block for ActivityNotFoundException should be around the function call "startActivity(url)" and not around the codes initializing the button.
Regards,
Kwok
Hi Geetha,
ReplyDeleteHope your doing well, Im sindhu im beginner in android, i had downloaded the open source code of CALENDAR_WIDGET,before starting my application i wanted to analyse google code, i was able trace from the activity mentioned in the manifest file, but im very confused with the act of the intent filter w.r.t its three things like data category & action, im not able to trace it, pls help me out how should i begin to anaylse any android code, since it has some of dependcy imnt able to debugg it.
can a receive a call from an application without clicking the answer button????
ReplyDeleteHi,
ReplyDeletecan we receive a call from an application without pressing the answer button???????
plz reply..........
Hello Geetha,
ReplyDeleteQuoting "Hi Lakmag,
Have you chose the correct target when you created your android project?
you need to select it as "Google APIs".
In an already created project, if you open the default.properties file, if you are seeing
"target=Google Inc.:Google APIs:7"
then, taget is right. If you are seeing
"target=android-7" then it will not work. Let me know if this helps. "
I am not seeing Google APIs:7 when I create new project. Can you help me please.
Thanks.
Hi Vijeth here,
ReplyDeleteAm learning android now, this tutorial is very nice. There is a small problem, i downloaded the code "NativeIntents2.1" from "http://www.mediafire.com/?mq1jmigrcwn" and run the code. All the activities are launching except the "browse" button. Its crashing with "Force Close" option.
Can anyone guide me....
Hi Vijeth,
ReplyDeleteI do not see your comment here but I received a mail with your "force close" problem on clicking browse. Here is my answer:
I have not done any parsing of URLs to ensure that you have entered a right format of the URL. So it will throw an exception if you do not enter the entire URL along with http:// included.
type a complete URL and click browse, it should work without any problem
Hi Sai Geetha,
ReplyDeleteThanks a lot i got it!!!
HI Sai Geetha,
ReplyDeleteI am trying to test call functionality on android dev, so can u pls help me by providing a script/apk that does this task automatically for the required no of times...
Thank you
Hi Sai Geeta,
ReplyDeleteI am trying to store 10 values through UI in to db using ContentProvider but it is not working.i saw lot of example they are given only 2 or 3 fileds are inserted in to db.any solution for this please give me the suggession.
Thanks in advance,
kumar
Hai Sai Geetha,
ReplyDeleteThanks a lot for this tutorial.I tried to execute the example application on ADT 2.2.All of them working well except google map.Whenever i am pressing search button it shows unexpected error.I think some more steps are needed to implement google map.Can you please help me to clarify my doupt.
hi sai geetha,
ReplyDeleteYour material works properly and you have done a good job.
I have a query.That is when I am using the code to call to a phone it gives an error as"your application stopped Unexpectedly" .Please help me whether it need any other requirement.
Thanks in advance
Hi Rani,
ReplyDeleteYou have to add the permission to made a call action.
Open AndroidMainfest.xml file -> Click Permision tab -> Add -> android.permission.CALL_PHONE.
Hope this will solve your problem.
Thanks
~Siddharth Pramanik
Hello Sai Geetha,
ReplyDeleteCan we invoke Navigation Application in Droid Phone from our Droid Application?
Hai Sai Geetha,
ReplyDeleteGoogle Map Intent application is not working
i want to share some more examples :
ReplyDeleteIntent.ACTION_CALL - tel:phone_number -Opens phone application and calls
phone_number.
Intent.ACTION_DIAL - tel:phone_number -Opens phone application and dials
(but doesn’t call) phone_number.
Intent.ACTION_DIAL- voicemail: -Opens phone application and dials
(but doesn’t call) the voice mail
number.
Intent.ACTION_VIEW- geo:lat,long -Opens the maps application centered
on (lat, long).
Intent.ACTION_VIEW- geo:0,0?q=address- Opens the maps application centered
on the specified address.
Intent.ACTION_VIEW- http://url
-Opens the browser application to the
specified address.
10
Intent.
ACTION_WEB_SEARCH-
plain_text- Opens the browser application and
uses Google search for given string.
Hi, I am facing "force close" problem on clicking view location .
ReplyDeleteHi,
ReplyDeletesuppose i want to call application "list" from application "buttonclick"
list and buttonclick are in different packages
list is in com.examples.List
and buttonclick is in com.examples.button
so i want to call app list when i press button from buttonclick activity
thanks in advance.
Hi Geetha,
ReplyDeleteVery nice blog can u explain is it possible to send sms from one emulator to another emulator of another machine which is connected with same network.
- Vivek
Thanks a lot your tutorial not only increases the interest but makes eager to learn basic concepts in Android
ReplyDeleteHai Geetha,
ReplyDeleteHere am working on Inbox lock(Default message application in android) application. i need idea r sample code yaar.....
A quantity of the most important factors is that influenced the growing demand for Android development company. Rize is powerful & instinctual platform for android apps development.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletesuppose i have 2 application install A and B so A is running currently and i want to call service define in B application using A so how to do this.
ReplyDeleteHi All,
ReplyDeleteMy android sdk 4.2.2 emulator is v v v slow due to its v low speed I am loosing focus.Can anyone help me
Thanks