Monday, August 14, 2017

android - Open dialer with phone number

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    android:background="#aca779"
    >
    <Button
        android:id="@+id/btn_do_task"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Dialer With Phone Number"
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private LinearLayout mRootLayout;
    private Button mBtnDoTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from xml layout
        mRootLayout = findViewById(R.id.root_layout);
        mBtnDoTask = findViewById(R.id.btn_do_task);

        // Set a click listener for the button
        mBtnDoTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openDialer();
            }
        });
    }

    // Custom method to open dialer app
    protected void openDialer(){
        /*
            Intent
                An intent is an abstract description of an operation to be performed. It can be
                used with startActivity to launch an Activity, broadcastIntent to send it to any
                interested BroadcastReceiver components, and startService(Intent) or
                bindService(Intent, ServiceConnection, int) to communicate with a background Service.

                An Intent provides a facility for performing late runtime binding between the code
                in different applications. Its most significant use is in the launching of
                activities, where it can be thought of as the glue between activities.
                It is basically a passive data structure holding an abstract description
                of an action to be performed.
        */
        /*
            String ACTION_DIAL
                Activity Action: Dial a number as specified by the data. This shows a UI with
                the number being dialed, allowing the user to explicitly initiate the call.

                Input: If nothing, an empty dialer is started; else getData() is URI of a phone
                number to be dialed or a tel: URI of an explicit phone number.

                Output: nothing.

                Constant Value: "android.intent.action.DIAL"
        */
        // Initialize an intent to open dialer app with specified phone number
        // It open the dialer app and allow user to call the number manually
        Intent intent = new Intent(Intent.ACTION_DIAL);
        // Send phone number to intent as data
        intent.setData(Uri.parse("tel:" + "+880XXXXXXXXXXXX"));
        // Start the dialer app activity with number
        startActivity(intent);
    }
}