Android tutorial : Managing phone calls


Most Android devices, if not all, are phones. So their users expect to be able to call and receive calls.
Android lets you handle the phone like any other component of the system.
Calling
To make a call from an application, using for example a number that you got from your own web service, create a DIAL ACTION intent with Uri of the form tel: NNNNN (where NNNNN is the phone number call) and use this intention with startActivity (). This does not initiate the call, but the activity will activate the handset, from which the user can then press a button to make the call.

The manager
use the  TelephonyManager class, which allows to:
  ● determine whether the phone is in use, via  getCallState () method, which returns :

  1.  CALL STATE IDLE (phone not used), 
  2. CALL STATE RINGING (connection on a call) 
  3. CALL STATE OFFHOOK (on a call);

● find the identifier of the SIM card with getSubscriberId ();
● know the type of phone (GSM, for example) with getPhoneType () or that of the connection (like GPRS, EDGE) with getNetworkType ().
In our example we simply use an input field to enter a phone number and a button to call the number.

<?xml version="1.0" encoding="utf-8"?>    
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"       
 android:layout_width="match_parent"     
   android:layout_height="match_parent"     
   android:layout_gravity="center_horizontal">

    <EditText
        android:layout_width="wrap_content"  
      android:layout_height="wrap_content"    

    android:id="@+id/number"    
    android:layout_alignParentTop="true"   
     android:layout_alignParentRight="true"    
    android:layout_alignParentEnd="true"     
   android:layout_alignParentLeft="true"    
    android:layout_alignParentStart="true"     
   android:hint="Telephone number" />

    <Button      
  android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:text="Call"  
      android:id="@+id/dial"  
      android:layout_below="@+id/number"      
  android:layout_centerHorizontal="true" />
</RelativeLayout>

Java code simply runs the handset using the number entered in the field:

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {
    EditText number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         number=(EditText)findViewById(R.id.number);
        Button dial=(Button)findViewById(R.id.dial);
        dial.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String toCall = "tel:" + number.getText().toString();
                startActivity(new Intent(Intent.ACTION_DIAL,
                        Uri.parse(toCall)));
            }
        });
    }

}
By clicking the call button this interface will be obtained

Commentaires

  1. comment il a afficher directement le clavier numérique sans que dans le xml on a pas spécifier le type de texte que on vas entrer ??

    RépondreSupprimer
  2. Essayes de bien lire la partie "calling" ... la réponse se trouve là bas

    RépondreSupprimer

Enregistrer un commentaire

Your comment will be reviewed by moderators

Posts les plus consultés de ce blog

Ionic 2 : Envoie d'une image vers un serveur web à l'aide de PHP et Angular 2

Premier tutoriel avec Arduino et Proteus ISIS

Login et password ,formulaire d'authentification sous android avec mysql et json