Connecting To Phone Book, Phone Call, And Default SMS Service using Android | In this tutorial, we’ll see how we can use different communication options such as phonebook, call and SMS services. For implementing the features, we’ll use Intent. The intent is used to establish communication between two applications. Hence, Intent will be used to link up the phonebook, phone or SMS service. Uri.parse creates an object of Uri telling the content provider about what we want to access by reference.

The output will be shown as:-

Download Project – [media-downloader media_id=”1255″ texts=”Download project”]

Download apk – [media-downloader media_id=”1256″ texts=”Download apk”]

Wants to Learn Advanced Android Application development from scratch- Beyond Basics

Step 1: Creating Android project
Open Android Studio -> Select New Project – >Select Blank Activity -> Click on Finish Button

Step 2: – Creating layout
As seen the layout consists of 4 buttons – one to link to the phonebook, second for linking the to a hone call and the third one for linking to the SMS service. We can also have an option to exit the app.
The XML file is as follows:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mytrendin.communication.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click To Open Phonebook"
        android:id="@+id/button1"
        android:layout_above="@+id/button2"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click To Call"
        android:id="@+id/button2"
        android:layout_above="@+id/button3"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click To Send SMS"
        android:id="@+id/button3"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Exit"
        android:id="@+id/button4"
        android:layout_below="@+id/button3"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

 

Step 3: Java- Creating Java file

As buttons are used for using the services, hence we need to do the coding using the setOnClickListener() method.
Inside this method, we’ll be using Intent for establishing communication with the respective application.

  • ACTION.VIEW is used for linking to the phonebook
  • ACTION.DIAL is for linking with the phone calls
  • ACTION.SENDTO is for sending text messages.

Related:

Receive SMS in Application and Search for keywords

Send SMS using Intent

What is the difference between IntentService and Service?

How to send data from one activity to another using getintent() ?

How to make a Call using Intent

We can start the service by using the method startActivity() whose argument contains the object of Intent of the respective service.
The java file is as follows:-

package com.mytrendin.communication;


        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 com.mytrendin.communication.R;

public class MainActivity extends AppCompatActivity
{

    Button btn1, btn2, btn3, btn4;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        btn3 = (Button) findViewById(R.id.button3);
        btn4 = (Button) findViewById(R.id.button4);

        btn1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people"));
                startActivity(i);

            }
        });

        btn2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"));
                startActivity(i);
            }
        });

        btn3.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:"));
                startActivity(i);

            }
        });

        btn4.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                finish();

            }
        });
    }

}

To exit the , we use the method – finish().

We do not have to take any special permissions for accessing the above services, so no changes are to be done in the AndroidManifest file. If You are Beginner Learn Android application development from the scratch

If you really liked the article, please subscribe to our YouTube Channel for videos related to this article.Please find us on Twitter and Facebook.

Related Posts