SlideShare une entreprise Scribd logo
1  sur  12
Développer sur Android
Android Lab Test
www.AndroidLabTest.com
Facebook
Par Bruno Delb
www.youtube.com/androidlabtest
www.twitter.com/brunodelb | www.facebook.com/brunodelb | blog.brunodelb.com
www.facebook.com/Androidlabtest
Youtube
Siteofficiel
Leçon : La téléphonie, les appels
La téléphonie, les appels
• Dans cette leçon, vous allez apprendre à intercepter les
appels téléphoniques.
• Pour cela, vous allez utiliser le gestionnaire de
téléphonie TelephonyManager.
La téléphonie, les appels
• Vous devez utiliser le gestionnaire de téléphonie avec
getSystemService.
TelephonyManager telephonyManager =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
• Enregistrez un listener PhoneStateListener.
MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
La téléphonie, les appels
• Gérez les événements (sonnerie en cours, décroché, …) dans la
méthode onCallStateChanged().
public class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch(state) {
case TelephonyManager.CALL_STATE_IDLE: break;
case TelephonyManager.CALL_STATE_RINGING: break;
case TelephonyManager.CALL_STATE_OFFHOOK: break;
}
}
}
La téléphonie, les appels
• En cas d’interruption de l’application (onPause() appelé), désactivez l’écoute avec
l’événement LISTEN_NONE.
public void onPause() {
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
• Lors de la reprise (onResume() appelé), réactivez l’écoute avec l’événement
LISTEN_CALL_STATE.
public void onResume() {
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
Layout main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="Call" />
<TextView
android:id="@+id/tv_log"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Fichier Main.java
public class Main extends Activity {
TextView tv_log = null;
TelephonyManager telephonyManager = null;
MyPhoneStateListener myPhoneStateListener = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv_log = (TextView)findViewById(R.id.tv_log);
Button btnCall = (Button)findViewById (R.id.btnCall);
btnCall.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:010101"));
startActivity(intent);
}
});
Fichier Main.java
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
myPhoneStateListener = new MyPhoneStateListener();
}
public void onResume() {
super.onResume();
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
public void onPause() {
super.onPause();
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
Fichier Main.java
public class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch(state) {
case TelephonyManager.CALL_STATE_IDLE:
tv_log.setText("Call state idle : " + incomingNumber); break;
case TelephonyManager.CALL_STATE_RINGING:
tv_log.setText("Call state ringing : " + incomingNumber); break;
case TelephonyManager.CALL_STATE_OFFHOOK:
tv_log.setText("Call state Offhook : " + incomingNumber); break;
default:
tv_log.setText("Call state other : " + incomingNumber); break;
}
}
}
}
Projet AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Testez sur votre mobile
Telephony_Calls
Retrouvez-moi sur ma chaîne AndroidLabTest …
Sur ma chaîne Youtube
https://www.youtube.com/user/mobiledevlabtest
Qui suis-je ?
Bruno Delb,
auteur du 1er livre francophone de développement d’application Java sur mobile (2002),
développeur d’applications mobiles & sociales,
parlez-moi de vos projets.
Et bien sûr sur mon site Web :
http://blog.brunodelb.com

Contenu connexe

Plus de Bruno Delb

Plus de Bruno Delb (20)

Introduction to Swift (tutorial)
Introduction to Swift (tutorial)Introduction to Swift (tutorial)
Introduction to Swift (tutorial)
 
Android Lab Test : Storage of data with SharedPreferences (english)
Android Lab Test : Storage of data with SharedPreferences (english)Android Lab Test : Storage of data with SharedPreferences (english)
Android Lab Test : Storage of data with SharedPreferences (english)
 
Android Lab Test : Using the sensor gyroscope (english)
Android Lab Test : Using the sensor gyroscope (english)Android Lab Test : Using the sensor gyroscope (english)
Android Lab Test : Using the sensor gyroscope (english)
 
Android Lab Test : Using the network with HTTP (english)
Android Lab Test : Using the network with HTTP (english)Android Lab Test : Using the network with HTTP (english)
Android Lab Test : Using the network with HTTP (english)
 
Android Lab Test : Managing sounds with SoundPool (english)
Android Lab Test : Managing sounds with SoundPool (english)Android Lab Test : Managing sounds with SoundPool (english)
Android Lab Test : Managing sounds with SoundPool (english)
 
Android Lab Test : Using the text-to-speech (english)
Android Lab Test : Using the text-to-speech (english)Android Lab Test : Using the text-to-speech (english)
Android Lab Test : Using the text-to-speech (english)
 
Android Lab Test : Reading the foot file list (english)
Android Lab Test : Reading the foot file list (english)Android Lab Test : Reading the foot file list (english)
Android Lab Test : Reading the foot file list (english)
 
Android Lab Test : Creating a menu dynamically (english)
Android Lab Test : Creating a menu dynamically (english)Android Lab Test : Creating a menu dynamically (english)
Android Lab Test : Creating a menu dynamically (english)
 
Android Lab Test : Creating a dialog Yes/No (english)
Android Lab Test : Creating a dialog Yes/No (english)Android Lab Test : Creating a dialog Yes/No (english)
Android Lab Test : Creating a dialog Yes/No (english)
 
Android Lab Test : The styles of views (english)
Android Lab Test : The styles of views (english)Android Lab Test : The styles of views (english)
Android Lab Test : The styles of views (english)
 
Android Lab Test : Creating a menu context (english)
Android Lab Test : Creating a menu context (english)Android Lab Test : Creating a menu context (english)
Android Lab Test : Creating a menu context (english)
 
Android Lab Test : Using the camera preview (english)
Android Lab Test : Using the camera preview (english)Android Lab Test : Using the camera preview (english)
Android Lab Test : Using the camera preview (english)
 
Android Lab Test : The views, the Gallery (english)
Android Lab Test : The views, the Gallery (english)Android Lab Test : The views, the Gallery (english)
Android Lab Test : The views, the Gallery (english)
 
Android Lab Test : Using the WIFI (english)
Android Lab Test : Using the WIFI (english)Android Lab Test : Using the WIFI (english)
Android Lab Test : Using the WIFI (english)
 
Android Lab Test : Managing the telephone calls (english)
Android Lab Test : Managing the telephone calls (english)Android Lab Test : Managing the telephone calls (english)
Android Lab Test : Managing the telephone calls (english)
 
Android Lab Test : Reading the SMS-inbox (english)
Android Lab Test : Reading the SMS-inbox (english)Android Lab Test : Reading the SMS-inbox (english)
Android Lab Test : Reading the SMS-inbox (english)
 
Android Lab Test : Installation of application in Java (english)
Android Lab Test : Installation of application in Java (english)Android Lab Test : Installation of application in Java (english)
Android Lab Test : Installation of application in Java (english)
 
Android Lab Test : Ecrire un texte sur le canevas (français)
Android Lab Test : Ecrire un texte sur le canevas (français)Android Lab Test : Ecrire un texte sur le canevas (français)
Android Lab Test : Ecrire un texte sur le canevas (français)
 
Android Lab Test : La connectivité réseau avec HTTP (français)
Android Lab Test : La connectivité réseau avec HTTP (français)Android Lab Test : La connectivité réseau avec HTTP (français)
Android Lab Test : La connectivité réseau avec HTTP (français)
 
Android Lab Test : Le capteur gyroscope (français)
Android Lab Test : Le capteur gyroscope (français)Android Lab Test : Le capteur gyroscope (français)
Android Lab Test : Le capteur gyroscope (français)
 

Dernier

Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptxCopie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
ikospam0
 

Dernier (12)

Echos libraries Burkina Faso newsletter 2024
Echos libraries Burkina Faso newsletter 2024Echos libraries Burkina Faso newsletter 2024
Echos libraries Burkina Faso newsletter 2024
 
Télécommunication et transport .pdfcours
Télécommunication et transport .pdfcoursTélécommunication et transport .pdfcours
Télécommunication et transport .pdfcours
 
python-Cours Officiel POO Python-m103.pdf
python-Cours Officiel POO Python-m103.pdfpython-Cours Officiel POO Python-m103.pdf
python-Cours Officiel POO Python-m103.pdf
 
L'expression du but : fiche et exercices niveau C1 FLE
L'expression du but : fiche et exercices  niveau C1 FLEL'expression du but : fiche et exercices  niveau C1 FLE
L'expression du but : fiche et exercices niveau C1 FLE
 
CompLit - Journal of European Literature, Arts and Society - n. 7 - Table of ...
CompLit - Journal of European Literature, Arts and Society - n. 7 - Table of ...CompLit - Journal of European Literature, Arts and Society - n. 7 - Table of ...
CompLit - Journal of European Literature, Arts and Society - n. 7 - Table of ...
 
Cours Généralités sur les systèmes informatiques
Cours Généralités sur les systèmes informatiquesCours Généralités sur les systèmes informatiques
Cours Généralités sur les systèmes informatiques
 
Formation échiquéenne jwhyCHESS, parallèle avec la planification de projet
Formation échiquéenne jwhyCHESS, parallèle avec la planification de projetFormation échiquéenne jwhyCHESS, parallèle avec la planification de projet
Formation échiquéenne jwhyCHESS, parallèle avec la planification de projet
 
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptxCopie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
 
Intégration des TICE dans l'enseignement de la Physique-Chimie.pptx
Intégration des TICE dans l'enseignement de la Physique-Chimie.pptxIntégration des TICE dans l'enseignement de la Physique-Chimie.pptx
Intégration des TICE dans l'enseignement de la Physique-Chimie.pptx
 
Neuvaine de la Pentecôte avec des textes de saint Jean Eudes
Neuvaine de la Pentecôte avec des textes de saint Jean EudesNeuvaine de la Pentecôte avec des textes de saint Jean Eudes
Neuvaine de la Pentecôte avec des textes de saint Jean Eudes
 
Saint Damien, missionnaire auprès des lépreux de Molokai, Hawaï.pptx
Saint Damien, missionnaire auprès des lépreux de Molokai, Hawaï.pptxSaint Damien, missionnaire auprès des lépreux de Molokai, Hawaï.pptx
Saint Damien, missionnaire auprès des lépreux de Molokai, Hawaï.pptx
 
RAPPORT DE STAGE D'INTERIM DE ATTIJARIWAFA BANK
RAPPORT DE STAGE D'INTERIM DE ATTIJARIWAFA BANKRAPPORT DE STAGE D'INTERIM DE ATTIJARIWAFA BANK
RAPPORT DE STAGE D'INTERIM DE ATTIJARIWAFA BANK
 

Android Lab Test : Les appels téléphoniques (français)

  • 1. Développer sur Android Android Lab Test www.AndroidLabTest.com Facebook Par Bruno Delb www.youtube.com/androidlabtest www.twitter.com/brunodelb | www.facebook.com/brunodelb | blog.brunodelb.com www.facebook.com/Androidlabtest Youtube Siteofficiel Leçon : La téléphonie, les appels
  • 2. La téléphonie, les appels • Dans cette leçon, vous allez apprendre à intercepter les appels téléphoniques. • Pour cela, vous allez utiliser le gestionnaire de téléphonie TelephonyManager.
  • 3. La téléphonie, les appels • Vous devez utiliser le gestionnaire de téléphonie avec getSystemService. TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); • Enregistrez un listener PhoneStateListener. MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener(); telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
  • 4. La téléphonie, les appels • Gérez les événements (sonnerie en cours, décroché, …) dans la méthode onCallStateChanged(). public class MyPhoneStateListener extends PhoneStateListener { public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch(state) { case TelephonyManager.CALL_STATE_IDLE: break; case TelephonyManager.CALL_STATE_RINGING: break; case TelephonyManager.CALL_STATE_OFFHOOK: break; } } }
  • 5. La téléphonie, les appels • En cas d’interruption de l’application (onPause() appelé), désactivez l’écoute avec l’événement LISTEN_NONE. public void onPause() { telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE); } • Lors de la reprise (onResume() appelé), réactivez l’écoute avec l’événement LISTEN_CALL_STATE. public void onResume() { telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); }
  • 6. Layout main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/btnCall" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="doClick" android:text="Call" /> <TextView android:id="@+id/tv_log" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
  • 7. Fichier Main.java public class Main extends Activity { TextView tv_log = null; TelephonyManager telephonyManager = null; MyPhoneStateListener myPhoneStateListener = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv_log = (TextView)findViewById(R.id.tv_log); Button btnCall = (Button)findViewById (R.id.btnCall); btnCall.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:010101")); startActivity(intent); } });
  • 8. Fichier Main.java telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); myPhoneStateListener = new MyPhoneStateListener(); } public void onResume() { super.onResume(); telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); } public void onPause() { super.onPause(); telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE); }
  • 9. Fichier Main.java public class MyPhoneStateListener extends PhoneStateListener { public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch(state) { case TelephonyManager.CALL_STATE_IDLE: tv_log.setText("Call state idle : " + incomingNumber); break; case TelephonyManager.CALL_STATE_RINGING: tv_log.setText("Call state ringing : " + incomingNumber); break; case TelephonyManager.CALL_STATE_OFFHOOK: tv_log.setText("Call state Offhook : " + incomingNumber); break; default: tv_log.setText("Call state other : " + incomingNumber); break; } } } }
  • 11. Testez sur votre mobile Telephony_Calls
  • 12. Retrouvez-moi sur ma chaîne AndroidLabTest … Sur ma chaîne Youtube https://www.youtube.com/user/mobiledevlabtest Qui suis-je ? Bruno Delb, auteur du 1er livre francophone de développement d’application Java sur mobile (2002), développeur d’applications mobiles & sociales, parlez-moi de vos projets. Et bien sûr sur mon site Web : http://blog.brunodelb.com