NOTE:

NOTE: Of late, I have been getting requests for very trivial problems that many of you are facing in your day-to-day work. This blog is not to solve your "project" problems - surely not a "Support" site.
I just love to share my knowledge in my spare time and would appreciate any questions or feedback on the articles and code I have shared. I also do appreciate thought-provoking questions that would lead me to write more articles and share.
But please do not put your day-to-day trivial problems here. Even if you do, you most probably would not get a response here.
Thanks

Search This Blog

x

Friday 28 August 2009

Pre-Packaged Applications | Android Tutorial for Beginners (Part 4)

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:

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

40 comments:

  1. Maybe instead "native" better
    "android system applications" ?

    If I see "native" I think about application that use NDK, or use JNI...

    Thanks.

    ReplyDelete
  2. Hi igor,

    Thanks 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

    ReplyDelete
    Replies
    1. 2. Browse the web for a given url:

      Intent 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

      Delete
  3. Hi Sai Geetha,
    thanks 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

    ReplyDelete
  4. Hi Osagie,

    have 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.

    ReplyDelete
  5. Thanks a lot Sai Geetha,
    that was just what I needed. I have now been able to include my custom view in main.xml.
    Cheers

    ReplyDelete
  6. thanks for this good tutorial
    :)

    ReplyDelete
  7. hi Nice articles.

    Could 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

    ReplyDelete
  8. Hi Kushal,

    This 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.

    ReplyDelete
  9. Hi sai,
    Im 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);

    ReplyDelete
  10. 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.

    ReplyDelete
  11. Hi sai geetha,
    Thanks 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.

    ReplyDelete
  12. Thanks a lot for this tutorial

    ReplyDelete
  13. Hi,

    have 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

    ReplyDelete
  14. Hi,

    I found out that the to be browsed web page url should start with "http://", otherwise a ActivityNotFoundException will be thrown.

    Regards,
    Kwok

    ReplyDelete
  15. Hi,

    Forgotten 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

    ReplyDelete
  16. Hi Geetha,
    Hope 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.

    ReplyDelete
  17. can a receive a call from an application without clicking the answer button????

    ReplyDelete
  18. Hi,
    can we receive a call from an application without pressing the answer button???????

    plz reply..........

    ReplyDelete
  19. Hello Geetha,

    Quoting "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.

    ReplyDelete
  20. Hi Vijeth here,

    Am 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....

    ReplyDelete
  21. Hi Vijeth,

    I 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

    ReplyDelete
  22. Hi Sai Geetha,

    Thanks a lot i got it!!!

    ReplyDelete
  23. HI Sai Geetha,

    I 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

    ReplyDelete
  24. Hi Sai Geeta,

    I 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

    ReplyDelete
  25. Hai Sai Geetha,

    Thanks 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.

    ReplyDelete
  26. hi sai geetha,

    Your 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

    ReplyDelete
  27. Hi Rani,

    You 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

    ReplyDelete
  28. Hello Sai Geetha,
    Can we invoke Navigation Application in Droid Phone from our Droid Application?

    ReplyDelete
  29. Hai Sai Geetha,
    Google Map Intent application is not working

    ReplyDelete
  30. i want to share some more examples :

    Intent.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.

    ReplyDelete
  31. Hi, I am facing "force close" problem on clicking view location .

    ReplyDelete
  32. Hi,
    suppose 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.

    ReplyDelete
  33. Hi Geetha,
    Very 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

    ReplyDelete
  34. Thanks a lot your tutorial not only increases the interest but makes eager to learn basic concepts in Android

    ReplyDelete
  35. Hai Geetha,
    Here am working on Inbox lock(Default message application in android) application. i need idea r sample code yaar.....

    ReplyDelete
  36. 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.

    ReplyDelete
  37. This comment has been removed by a blog administrator.

    ReplyDelete
  38. suppose 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.

    ReplyDelete
  39. Hi All,
    My 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

    ReplyDelete