Showing posts with label Android Examples. Show all posts
Showing posts with label Android Examples. Show all posts

Saturday, June 23, 2012

Pre-Packaged Applications | Android Tutorial for Beginners

There are many applications that are pre-packaged in the android platform that can be reused by the other custom applications. As we have stated in the previous  part 3. Implicit intents there we have provided the example of these types of one application related to contact application of android.


In this part of tutorial we will explore that how we can use the pre-packaged applications using impliocit intents. In these types of application we will just call the pre-packaged application using intents and pass them to the activity and after then we will start our activity with the startActivity() method. However, for each of these intents, the platform find the most befitting activity and invoke the same:

1. Call a number(Dial a number via application)
      Intent callNumber = new Intent();
      callNumber.setAction(android.content.Intent.ACTION_CALL);
      callNumber.setData(Uri.parse("tel:123456789"));
startActivity(callNumber);

This will call the number 123456789.If you want to call any number you want then you have to just add a edit text control then pass the desired value to the Uri.parse() method then it will dial your specified number.


 
2. Browse the web for a given url:

      Intent searchText = new Intent(Intent.ACTION_WEB_SEARCH);
      searchText.putExtra(SearchManager.QUERY, "Android Demo);
      startActivity(searchText);

This will start the google search  engine to search the specified string "Android Demo" and the get back the result on the screen. here the same thing as above specified in the dialed number application you can define a edit text control the pass the desired string to the appropiate space in the putExtra() method then the same result will show on the screen.


3.  View google maps for a given location(find out any location)

      Intent searchLocation = new Intent(Intent.ACTION_VIEW, 
      Uri.parse("geo:0,0?q=India"));
startActivity(searchLocation);

This will show the "India" location on the Google map. You can repeat the smae method for the edit text to search your desired location.


4. View Contacts on the phone(Navigate to view contacts)

Intent contacts = new Intent();
      contacts.setAction(android.content.Intent.ACTION_VIEW);
      contacts.setData(ContactsContract.Contacts.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.



Saturday, June 9, 2012

Implicit Intent | Android Tutorial for Beginners



We have seen in the last post about the explicit intents with a very simple example. now we will move to the more interesting concept implicit intents.

This require more theoretical aspects to understand it more clearly.


As we know that, explicit intents need a target activity to execute those type of intents. Here implicit intents does not need the target activity to define over here. 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?

 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 here the android platform compare these three (action, category and data) to something called "intent filters" are declared by probable target who are willing to accept implicit intents.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.

      Note: Intent filters are always define in the android manifest files. Like our first activity which is called first time when application starts.
Some important points in Implicit intents.
  1. Implicit intent never define target components.
  2.  Components willing to receive implicit intent have to declare to handle their specific intent by declaring intent filters.
  3. A component can declare any number of Intent Filters 
  4. There can be more than one component that declares the same Intent Filters.
  5. You can set priority of intent filters to ensure order and responses. 
There are 3 tests conducted in order to match an intent with intent filters:
  1. Action Test
  2. Category Test
  3. Data Test 
 To see how the implicit intent is defined and working please download the code from here.

The ImplicitIntent 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 "ContactsContract.Contacts.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 showContacts = (Button) findViewById(R.id.clickhere);
            showContacts.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                Intent contacts = new Intent();
                contacts.setAction(android.content.Intent.ACTION_VIEW);
                contacts.setData(ContactsContract.Contacts.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.
Here is clearly shown that here no any other target component is defined. here is just a intent filter is set . on click of the button the internal contacts app is going to open.