Develop on Android
Android Lab Test
www.AndroidLabTest.com
Youku
By Bruno Delb
www.weibo.com/brunodelb
i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn
http://i.youku.com/brunoparis
Weibo
Siteofficiel
Lesson : Telephony, calls
Telephony, the calls
• In this lesson, you will learn to intercept incoming calls.
• For this, you will learn to use the telephony manager
TelephonyManager.
Telephony, the calls
• You have to use the telephony manager with getSystemService.
TelephonyManager telephonyManager =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
• Register a listener PhoneStateListener.
MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Telephony, the calls
• Manage the events (ringing, hang up, …) in the method
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;
}
}
}
Telephony, the calls
• When the application is interrupted (onPause() called), unregister the listening with
the event LISTEN_NONE.
public void onPause() {
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
• When the application is resumed (onResume() called), re-register the listening with
the event 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>
File 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);
}
});
File 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);
}
File 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;
}
}
}
}
Project AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Test on your mobile
Telephony_Calls
Follow me on my channel PengYooTV …
On my Youku channel
http://i.youku.com/brunoparis
Who am I ?
Bruno Delb (www.delb.cn),
Author of the first french book of development of Java mobile application (2002),
Consultant, project manager and developer of social & mobile applications,
let’s talk about your needs ...
And on Weibo :
http://www.weibo.com/brunodelb

Android Lab Test : Managing the telephone calls (english)

  • 1.
    Develop on Android AndroidLab Test www.AndroidLabTest.com Youku By Bruno Delb www.weibo.com/brunodelb i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn http://i.youku.com/brunoparis Weibo Siteofficiel Lesson : Telephony, calls
  • 2.
    Telephony, the calls •In this lesson, you will learn to intercept incoming calls. • For this, you will learn to use the telephony manager TelephonyManager.
  • 3.
    Telephony, the calls •You have to use the telephony manager with getSystemService. TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); • Register a listener PhoneStateListener. MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener(); telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
  • 4.
    Telephony, the calls •Manage the events (ringing, hang up, …) in the method 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.
    Telephony, the calls •When the application is interrupted (onPause() called), unregister the listening with the event LISTEN_NONE. public void onPause() { telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE); } • When the application is resumed (onResume() called), re-register the listening with the event 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.
    File Main.java public classMain 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.
    File 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.
    File Main.java public classMyPhoneStateListener 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; } } } }
  • 10.
  • 11.
    Test on yourmobile Telephony_Calls
  • 12.
    Follow me onmy channel PengYooTV … On my Youku channel http://i.youku.com/brunoparis Who am I ? Bruno Delb (www.delb.cn), Author of the first french book of development of Java mobile application (2002), Consultant, project manager and developer of social & mobile applications, let’s talk about your needs ... And on Weibo : http://www.weibo.com/brunodelb