监听android电话呼叫状态
android-监听电话呼叫状态
01 |
在清单文件AndroidManifest.xml中添加权限: |
02 |
<uses-permission android:name= "android.permission.READ_PHONE_STATE" /> |
03 |
|
04 |
/* 取得电话服务 */ |
05 |
TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
06 |
//监听电话的状态 |
07 |
telManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); |
08 |
|
09 |
PhoneStateListener listener = new PhoneStateListener(){ |
10 |
@Override public void onCallStateChanged(int state, String incomingNumber) { |
11 |
switch (state){ |
12 |
case TelephonyManager.CALL_STATE_IDLE: /* 无任何状态时 */ |
13 |
break; |
14 |
case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起电话时 */ |
15 |
break; |
16 |
case TelephonyManager.CALL_STATE_RINGING: /* 电话进来时 */ |
17 |
break ; |
18 |
default : |
19 |
break ; |
20 |
} |
21 |
super .onCallStateChanged(state, incomingNumber); |
22 |
} |
23 |
}; |
一起学习GIS及其二次开发,一起进步!