Monday, 12 December 2016

AndroidPhoneCall

Android gives Built-in applications to telephone calls, in a few events we may need to make a telephone call through our application. This should effectively be possible by utilizing verifiable Intent with fitting activities. Likewise, we can utilize PhoneStateListener and Telephony Manager classes, keeping in mind the end goal to screen the adjustments in some communication states on the gadget.

In AndroidMainfest.XML, we should add uses-permission  for Phone Call State as shown below,

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

Activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:textAlignment="center">
 
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android PHONE Call " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="call" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:inputType="phone" 
 android:ems="10" 
 android:layout_marginTop="20dp" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" />
</LinearLayout> 

MainActivity.java :-

package com.kiranapp;

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;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    Button callbtn;
    EditText ebtn;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 callbtn = (Button) findViewById(R.id.button);
 ebtn = (EditText) findViewById(R.id.editText);
 callbtn.setOnClickListener(new View.OnClickListener() {
 
@Override 
 public void onClick(View v) {
 String s1 = ebtn.getText().toString();
 Intent callIntent = new Intent(Intent.ACTION_CALL);
 callIntent.setData(Uri.parse("tel:" + s1));
 if (ActivityCompat.checkSelfPermission(MainActivity.this,
         Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling  // 
 ActivityCompat#requestPermissions 
 // here to request the missing permissions, and then overriding // 
 public void onRequestPermissionsResult(int requestCode, String[] permissions,
 // int[] grantResults) 
 // to handle the case where the user grants the permission. See the documentation 
 // for ActivityCompat#requestPermissions for more details. //
 return;
 }
 startActivity(callIntent);

 }
 });

 }

 }

Output :-

Android phone call
Android PhoneCall figure

1 comment: