Teaching phone calls in Android programming

Android; Provides built-in applications for phone calls; In some situations, we may need to use our app; Make a phone call. This can be done simply by using tacit internet with appropriate actions; 

phone calls.  We can use the PhoneStateListener and TelephonyManager classes to monitor changes in some phone conditions on the device.

This chapter lists all the simple steps you need to take to create an app that can make phone calls; To be adopted. You call the Android internal phone call function; You can use Android Internet to make a phone call.

Internet-object operation to make a phone call

You are using ACTION_CALL to trigger the internal phone call function on your Android device. Simple syntax below; Used to create an Internet with ACTION_CALL:

Intent phoneIntent = new Intent (Intent.ACTION_CALL);

You can use ACTION_DIAL instead of ACTION_CALL; In this case, you will have the option to create a hard-coded phone number before making a call, instead of making a direct call; correct.

Internet type / data to make a phone call

To make a phone call to the given phone number 91,000-000,000,000; Should be as follows; Specify the tel as a URI using the setData () method.

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

 

Interestingly, to make a phone call; You do not need to specify any additional data or data type.

Example

The following example shows you practically how to use the Android Internet to make a phone call to a given phone number; use.

To test this example, you need a real phone with the latest version of the Android operating system; otherwise; You are probably getting into trouble with the emulator because it may not work properly.

the level Explanation
1 You have to create an Android application from Android Studio IDE you use it under a package com.example.saira_000.myapplication as MyApplication appropriately.
۲ Modify the src / mainActivity.java file and add the code needed to take care of the call
3 Modify XML file paging res / layout / activity_main.xml Adds any required GUI component. I just added a simple button to call 91000-000000-0000.
4 There is no need to define default string constants in res / values ​​/ strings.xml. Android Studio takes care of the default constants.
5 Modify AndroidManifest.xml as shown below.
6 Run the application to launch the Android emulator and confirm that changes have been made to the application.

Below you can see the contents of the modified src / MainActvity.java main activity file:

package com.example.saira_000.myapplication;

import android.Manifest;

import android.content.Intent;

import android.content.pm.PackageManager;

import android.net.Uri;

import android.os.Bundle;

import android.support.v4.app.ActivityCompat;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private Button button;

@Override

protected void onCreate (Bundle savedInstanceState) {

super.onCreate (savedInstanceState);

setContentView (R.layout.activity_main);

button = (Button) findViewById (R.id.buttonCall);

button.setOnClickListener (new View.OnClickListener () {

public void onClick (View arg0) {

Intent callIntent = new Intent (Intent.ACTION_CALL);

callIntent.setData (Uri.parse (“tel: 0377778888”));

if (ActivityCompat.checkSelfPermission (MainActivity.this,

Manifest.permission.CALL_PHONE)! = PackageManager.PERMISSION_GRANTED) {

return;

}

startActivity (callIntent);

}

});

}

}

You can also see the contents of the res / layout / activity_main.xml file below.

<? 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 / buttonCall”

android: layout_width = ”wrap_content”

android: layout_height = ”wrap_content”

android: text = ”call 0377778888 ″ />

</LinearLayout>

The following is the content of res / values ​​/ strings.xml to define two new constants:

<? xml version = ”1.0 ″ encoding =” utf-8 ″?>

<resources>

<string name = ”app_name”> My Application </string>

</resources>

The following section shows the default AndroidManifest.cml content:

<? xml version = ”1.0 ″ encoding =” utf-8 ″?>

<manifest xmlns: android = ”http://schemas.android.com/apk/res/android”

package = ”com.example.saira_000.myapplication”>

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

<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>

Let’s try launching your My Application. Suppose you have connected your real Android phone to your computer.

To run the application from Android Studio; Open one of your project activity files and click on the run iconC: \ Users \ mohammad \ Downloads \ eclipse_run.jpgClick on the toolbar. Select your mobile device as an option and then check your mobile device which shows the following screen:

C: \ Users \ mohammad \ Downloads \ call12.jpg

Now use the Call button as you can see below; Make a phone call:

C: \ Users \ mohammad \ Downloads \ call2.jpg

Leave a Reply

Your email address will not be published. Required fields are marked *