Sunday, August 15, 2010

Working with Android. Chapter 2: Intent Class

In android, it is possible to open a webpage (or another application) from your application, which can be accomplished using the class Intent. Intent is a very useful class, which will allow you to perform a lot of different operations such as dial a phone,  install new packages, view your contacts list, pick data from other applications and so on. An intent is an abstract description of an operation to be performed. Used with startActivity launches an application, used with startService makes it possible to communicate with a background service.

 In this post, three examples of Intent class are given for different operations.


1. Opening a Webpage with ACTION_VIEW:
The ACTION_VIEW intent displays data to the user.  For example, when used on a contacts entry it will view the entry; when used on a mailto: URI it will bring up a compose window filled with the information supplied by the URI; when used with a tel: URI will invoke the dialer (more information on URI here http://developer.android.com/reference/android/net/Uri.html)

       String url = "http://www.google.com"; //define url to load
       Intent i = new Intent(Intent.ACTION_VIEW); //create an Intent.ACTION_VIEW
       Uri u = Uri.parse(url); //creates the Uri object
       i.setData(u);  //
Set the data this intent is operating on.
       try {
                 startActivity(i); //
Launch a new activity
       } catch (ActivityNotFoundException e){ //in case something did not work
                 Toast toast = Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT);
       }
As usual, you will have to add internet permission to your manifest.xml:
     

2. Calling another Application from your main application with startActivityForResult:
The function startActivityForResult will launch an activity for which you would like a result when it finished. When this activity exits, your onActivityResult() method will be called with the given requestCode.

      int ACTIVITY_INVOKE = 0;
      Intent i = new Intent(); //creates a new intent
      i.setClassName("com.android.app2","com.android.app2.Application2"); //specify app2
      startActivityForResult(i, ACTIVITY_INVOKE);

setClassName(String packageName, String className) allows you to specify the application package name and class name of the application you want to call (which has to be already installed in your phone).  The Package name corresponds to the directory list in /src/ and your class name is . (without the .java extension!).
 startActivityForResult(Intent intent, int RequestCode) will call the application specified with i.setClassName and call the method onActivityResult() with the given requestCode when it finished.
Finally, it is possible to override the onActivityResult method to fit your needs:

      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) //when the activity is finished
      {
            if(resultCode==0) { //ok
                  Toast toast = Toast.makeText(this, "All ok", Toast.LENGTH_SHORT);
                  toast.show();
            } else { //something went wrong
                  Toast toast = Toast.makeText(this, "Firma no aceptada", Toast.LENGTH_SHORT);
                  toast.show();
            }
      }

3. Dialing a phone with ACTION_DIAL
Using startActivity and  setAction, it is possible to dial a phone (ACTION_DIAL) or to make a call (ACTION_CALL):

      Intent i = new Intent();
      i.setAction(Intent.ACTION_DIAL); //set action to be performed
      i.setData(Uri.parse("tel:+34000000000")); //specify the phone number
     startActivity(i); //starts activity