標籤:android style blog http color ar os sp div
android sdk 提供很多公用的服務,也就是系統服務,開發人員可以通過Activity類的getSystemService方法擷取指定的服務。系統服務包含音頻服務、視頻服務視窗服務等。本篇主要講Telephony_Service.該服務用來監聽通話的狀態。
1.擷取telphony_mannager對象
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
MyPhoneCallListener listener=new MyPhoneCallListener();
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
擷取tm對象,並實現該對象的監聽
2.定義監聽方法
1 public class MyPhoneCallListener extends PhoneStateListener{ 2 public void onCallStateChanged(int state,String incomingnae) 3 { 4 switch(state) 5 { 6 case TelephonyManager.CALL_STATE_OFFHOOK: 7 Toast.makeText(MainActivity.this, "正在通話中。。。", Toast.LENGTH_LONG).show(); 8 break; 9 case TelephonyManager.CALL_STATE_RINGING:10 Toast.makeText(MainActivity.this,incomingnae, Toast.LENGTH_LONG).show();11 break;12 }13 super.onCallStateChanged(state, incomingnae);14 }15 }Myphonelistener
自訂監聽方法。
Android Service 系統服務