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.
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:background="#FFFFFF"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/buttonwebbrowser"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Web Browser"
- android:onClick="onClickBrowseWeb"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/buttondocall"
- android:text="Call Here"
- android:onClick="onClickCallHere"
-
- />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/buttondisplaymap"
- android:text="Display Map"
- android:onClick="onClickDisplayMap"
- />
- </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.
- package com.example.administrator.myfragmentapp;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.content.Intent;
- import android.net.Uri;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Display;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.WindowManager;
- public class MainActivity extends ActionBarActivity
- {
- int reqcode = 1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void onClickBrowseWeb(View view) {
- Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.in"));
- startActivity(intent);
- }
- public void onClickCallHere(View view) {
- Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:+91xxxxxxxxxx"));
- startActivity(intent);
- }
- public void onClickDisplayMap(View view) {
- Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:28.7041,77.1025"));
- startActivity(intent);
- }
- }
To see the result, Run the Application by clicking Shift + F10 on the Android emulator.
Now, click on first option. Google page will open in the Browser.
Click the second option. It will open a dial up option to make a call to the specified mobile number.
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.