Comment passer un appel téléphonique sous Android

Comment passer un appel téléphonique sur Android

Dans ce didacticiel, nous vous montrons comment passer un appel téléphonique sous Android et surveiller les états des appels téléphoniques viaPhoneStateListener.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1 Fichiers de mise en page Android

Fichier de mise en page Simpel, pour afficher un bouton.

Fichier: res / layout / main.xml




    

2. Activité

Utilisez l'extrait de code ci-dessous pour passer un appel téléphonique sur Android.

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:0377778888"));
    startActivity(callIntent);

File : MainActivity.java - Lorsque le bouton est appelé, appelez le 0377778888.

package com.example.android;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button button;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        // add button listener
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:0377778888"));
                startActivity(callIntent);

            }

        });

    }

}

3 Manifeste Android

Pour passer un appel téléphonique, Android a besoin de l'autorisation deCALL_PHONE.

Fichier: AndroidManifest.xml




    

    

    

        
            
                
                
            
        
    

4. Exemple PhoneStateListener

Ok, maintenant nous mettons à jour l'activité ci-dessus, pour surveiller les états des appels téléphoniques, lorsqu'un appel téléphonique est terminé, revenez à l'activité d'origine (en fait, il suffit de redémarrer l'activité). Lisez le commentaire, il devrait être explicite.

Note
Exécutez-le et reportez-vous à la console logcat pour comprendre le fonctionnement dePhoneStateListener.

Fichier: MainActivity.java

package com.example.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    final Context context = this;
    private Button button;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        // add PhoneStateListener
        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);

        // add button listener
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:0377778888"));
                startActivity(callIntent);

            }

        });

    }

    //monitor phone call activities
    private class PhoneCallListener extends PhoneStateListener {

        private boolean isPhoneCalling = false;

        String LOG_TAG = "LOGGING 123";

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (TelephonyManager.CALL_STATE_RINGING == state) {
                // phone ringing
                Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
            }

            if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                // active
                Log.i(LOG_TAG, "OFFHOOK");

                isPhoneCalling = true;
            }

            if (TelephonyManager.CALL_STATE_IDLE == state) {
                // run when class initial and phone call ended,
                // need detect flag from CALL_STATE_OFFHOOK
                Log.i(LOG_TAG, "IDLE");

                if (isPhoneCalling) {

                    Log.i(LOG_TAG, "restart app");

                    // restart app
                    Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(
                            getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);

                    isPhoneCalling = false;
                }

            }
        }
    }

}

Mettez à nouveau à jour le fichier manifeste Android,PhoneStateListener a besoin de l'autorisation deREAD_PHONE_STATE.

Fichier: AndroidManifest.xml




    

    
    

    

        
            
                
                
            
        
    

5. Demo

L'activité a commencé, affichez simplement un bouton.

android phone call example

Lorsque vous cliquez sur le bouton, appelez le 0377778888.

android phone call example

Lorsque l'appel téléphonique est suspendu ou terminé, redémarrez l'activité principale.

android phone call example

Télécharger le code source

Téléchargez-le -Android-Make-Phone-Call-Example.zip (16 Ko)