Tuesday, 15 March 2016

Opening phone dialer from an Android application

Opening phone dialer from an Android application


Phone dialer is an activity available with Android operating system to call a number. In this article, we will create an application which invokes the dialer on a button click.  Android system provides a standard action name called “ACTION_DIAL” to invoke the dialer. We will make use this action to invoke the the dialer from our application.

1. Create a new Android project namely “DialerDemo”
New Android Project
Figure 1 : New Android Project

2. Select build target for the application
Android Build Target
Figure 2: Android Build Target

3. Enter application details
Application Details
Figure 3: Application Details

4. res/values/strings.xml
1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">DialerDemo</string>
    <string name="dialer">Open Dialer</string>
</resources>

5. res/layout/main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<Button
    android:id="@+id/btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/dialer" />
</LinearLayout>

6. src/in/wptrafficanalyzer/dialerdemo/MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package in.wptrafficanalyzer.dialerdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /** Referring to the button object of the main.xml layout */
        Button btn  = (Button) findViewById(R.id.btn);
        /**    Defining a click event listener    */
        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                /** Creating an intent with the dialer's action name  */
                /** Since the intent is created with activity's action name, the intent is an implicit intent */
                Intent intent = new Intent("android.intent.action.DIAL");
                /** Starting the Dialer activity */
                startActivity(intent);
            }
        };
        /** Setting click event listener for the buttonS */
        btn.setOnClickListener(listener);
    }
}


Main Activity
Figure 4: Main Activity
  • On clicking the button "Open Dialer", the given below "Phone Dialer" will be opened"
Dialer Activity

No comments:

Post a Comment