Showing posts with label android intents. Show all posts
Showing posts with label android intents. 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.



Tuesday, June 19, 2012

Explicit Intent | Android Tutorial for Beginners

Explicit Intent I have described  intents earlier. Here we will discuss about intents, Intents are the methods to make a communication between two activities. means they used to pass data from one activity to other with the help of some variables.

There are 2 types of intents that Android understands.
1. Explicit Intent
2. Implicit Intent

In an Explicit intent, you actually specify the activity that is required to respond to the intent. In other words, you explicitly designate the target component(Activity). This is typically used for application internal messages.
Like we have to pass data from one activity to other activity to accomplish certain processing we used explicit intents.

In an Implicit intent (the main power of the android design), you just declare an intent and leave it to the platform to find an activity that can respond to the intent. Here, you do not declare the target component and hence is typically used for activating components of other applications seamlessly.


Now to explain intents in more detail you can download my example from here.


        Button invokeIntent = (Button)findViewById(R.id.invokeintent);
       
invokeIntent.setOnClickListener(new OnClickListener() {
        

        
public void onClick(View v) {
        
Intent explicitIntent = new Intent(InvokingActivity.this,InvokedActivity.class);
         startActivity(explicitIntent);
        
}
        });

 Note: Here the InvokingActivity activity this class have the layout main.xml and the  InvokedActivity this activity have the InvokedActivity.xml file for there layout. both of the layout you can make by using eclipse.

 In the next part of the series, we will see how to work with implicit intents which also needs us to understand intent-filters.