Sunday , October 21 2018
Home / Android / Android – Make Phone Calls

Android – Make Phone Calls

Android provides Built-in applications for phone calls, in some occasions we may need to make a phone call through our application. This could easily be done by using implicit Intent with appropriate actions. Also, we can use PhoneStateListener and TelephonyManager classes, in order to monitor the changes in some telephony states on the device.

This chapter lists down all the simple steps to create an application which can be used to make a Phone Call. You can use Android Intent to make phone call by calling built-in Phone Call functionality of the Android. Following section explains different parts of our Intent object required to make a call.

Action to make Phone Call

Intent phoneIntent = new Intent(Intent.ACTION_CALL);

You can use ACTION_DIAL action instead of ACTION_CALL, in that case you will have option to modify hardcoded phone number before making a call instead of making a direct call.

Data/Type to make Phone Call

phoneIntent.setData(Uri.parse(“tel:91-000-000-0000″));

Example

Following example shows you in practical how to use Android Intent to make phone call to the given mobile number.

MainActivity.java

package com.example.saira_000.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
call();
}

private void call() {
Intent in=new Intent(Intent.ACTION_CALL,Uri.parse(“0000000000″));
try{
startActivity(in);
}

catch (android.content.ActivityNotFoundException ex){
Toast.makeText(getApplicationContext(),”yourActivity is not founded”,Toast.LENGTH_SHORT).show();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml

<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:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”W2class”
android:id=”@+id/textView2″
android:layout_below=”@+id/textView”
android:layout_centerHorizontal=”true”
android:textSize=”30dp”
android:textColor=”#ff14be3c” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Call”
android:id=”@+id/button”
android:layout_below=”@+id/imageView”
android:layout_alignRight=”@+id/textView2″
android:layout_alignEnd=”@+id/textView2″
android:layout_marginTop=”54dp”
android:layout_alignLeft=”@+id/imageView”
android:layout_alignStart=”@+id/imageView” />
</RelativeLayout>
res/values/strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
<string name=”app_name”>My Application</string>
<string name=”hello_world”>Hello world!</string>
<string name=”action_settings”>Settings</string>
</resources>

AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.saira_000.myapplication”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-permission android:name=”android.permission.CALL_PHONE” />
<uses-permission android:name=”android.permission.READ_PHONE_STATE” />

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >

<activity
android:name=”com.example.saira_000.myapplication.MainActivity”
android:label=”@string/app_name” >

<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>

</activity>

</application>
</manifest>

Lets try to run your application. You have to sure about that you connected your real device(your phone) to the system and then run your application.When you click on the button in the application Automatically make a call to number which you specified in code.

Preview

call121

call12

 

About admin

Check Also

Android Activity Launch Modes

Launch mode is an instruction for Android OS which specifies how the activity should be ...

Leave a Reply