Calling In Built Functions In Android Application

Overview

Hope, you got a chance to read some useful articles to start programming in Android Applications. Below are some useful links,

Introduction

In this article, I will explain, how to call some built in functions in Android Applications.

I have already discussed, how to call THE activities within your own applications. One of the key aspects of Android programming is using the intent to call activities from other Applications. An Application can call many built in Applications, which are included with an Android device. Suppose, you want to load a Web page, which is not part of your Application. You can use the Intent object to invoke the built-in Web Browser to display the Web page, instead of building your own Web Browser for this purpose.

Coding

Add three buttons are in activity_main.xml file as Web Browser. Call here and display Map.

  1. <LinearLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="fill_parent"  
  6. android:orientation="vertical"  
  7. android:background="#FFFFFF"  
  8. tools:context=".MainActivity">  
  9.     <Button  
  10. android:id="@+id/buttonwebbrowser"  
  11. android:layout_width="fill_parent"  
  12. android:layout_height="wrap_content"  
  13. android:text="Web Browser"  
  14. android:onClick="onClickBrowseWeb"/>  
  15.     <Button  
  16. android:layout_width="fill_parent"  
  17. android:layout_height="wrap_content"  
  18. android:id="@+id/buttondocall"  
  19. android:text="Call Here"  
  20. android:onClick="onClickCallHere"  
  21.   
  22. />  
  23.     <Button  
  24. android:layout_width="fill_parent"  
  25. android:layout_height="wrap_content"  
  26. android:id="@+id/buttondisplaymap"  
  27. android:text="Display Map"  
  28. android:onClick="onClickDisplayMap"  
  29. />  
  30. </LinearLayout>   

Add three methods are in MainActivity.java class to display the Website in the Web Browser. Call on any mobile number  and display the map by Google maps.

  1. package com.example.administrator.myfragmentapp;  
  2. import android.app.FragmentManager;  
  3. import android.app.FragmentTransaction;  
  4. import android.content.Intent;  
  5. import android.net.Uri;  
  6. import android.support.v7.app.ActionBarActivity;  
  7. import android.os.Bundle;  
  8. import android.view.Display;  
  9. import android.view.Menu;  
  10. import android.view.MenuItem;  
  11. import android.view.View;  
  12. import android.view.WindowManager;  
  13. public class MainActivity extends ActionBarActivity   
  14. {  
  15.     int reqcode = 1;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.     }  
  21.     public void onClickBrowseWeb(View view) {  
  22.         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.in"));  
  23.         startActivity(intent);  
  24.     }  
  25.     public void onClickCallHere(View view) {  
  26.         Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:+91xxxxxxxxxx"));  
  27.         startActivity(intent);  
  28.     }  
  29.     public void onClickDisplayMap(View view) {  
  30.         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:28.7041,77.1025"));  
  31.         startActivity(intent);  
  32.     }  
  33. }   
To see the result, Run the Application by clicking Shift + F10 on the Android emulator.

emulator

Now, click on first option. Google page will open in the Browser.

browser

Click the second option. It will open a dial up option to make a call to the specified mobile number.

dialup

To load the Maps Application, click the display map button. To display the Maps Application, you need to run the Application on an AVD , which supports the Google APIs or you have to run this app on your device.

Explanation

In this app, we have used the Intent class to invoke some of the built-in Applications in Android like Browser, Phone and Maps).

In Android, intents usually come in pairs: action and data. The action describes, what is to be performed, such as editing an item, viewing the content of an item and so on. The data specifies, what is affected, such as a person in the specified database. The data is specified as an URL object.

Some examples of the actions are: 

  • ACTION_VIEW
  • ACTION_DIAL
  • ACTION_PICK 

Some examples of the data include: 

  • www.google.co.in
  • tel:+91-xxxxxxxxxx
  • geo:28.7041,77.1025 

In the first button, create an object of an Intent and then pass the two arguments to its constructor. The action and the data will be:

Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.in"));
startActivity(intent);

The action, here is represented by the Intent.ACION_VIEW constant. We used the parse() method of the URL class to convert a URL string into a URL object.

For the second button, we can dial a specific number by passing in the telephone number in the portion.

Intent intent=new Intent(Intent.ACTION_DIAL, URL.parse("tel:+91xxxxxxxxxx"));
startActivity(intent);

In this case, the dialler will display the number to be called. The user must still click the dial button to dial the number. If you want to directly call the number without the user intervention, change the action, given below:

Intent intent=new Intent(Intent.ACTION_CALL, URL.parse("tel:+91xxxxxxxxxx"));
startActivity(intent);

For this, you need to assign the Android.permission.CALL_PHONE permission to your Application, defined in the manifest file.

You can simply omit the data portion to display the dialer without specifying any number.

Intent intent=new Intent(Intent.ACTION_DIAL);
startActivity(intent);

The third button displays a map using the ACTION_VIEW constant. Here we use “geo” in place of “http”.

Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("geo:28.7041,77.1025"));
startActivity(intent);

Conclusion

In this article, I explained about calling some built-in Applications, using Intents. In the next article, I will explain about an Intent object and intent filters in detail. If you have any question or comment, post a message in C# Corner comments section.