PhoneStateListener
1.對特定的電話狀態的監聽,包括服務的狀態、信號強度、消息等待指示(語音信箱)、通話轉移、呼叫狀態、設備單元位置、數據連接狀態、數據流量方向。一些電話信息受權限保護,應用程序不會收到受保護的信息的更新,除非在manifest文件中有適當的權限聲明。凡申請許可,有適當的LISTEN_標志。
2.對監聽的話做處理
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case LISTEN_SERVICE_STATE:/*Listen for changes to the network service state (cellular).對網絡服務狀態監聽*/
PhoneStateListener.this.onServiceStateChanged((ServiceState)msg.obj);
break;
case LISTEN_SIGNAL_STRENGTH:/*Listen for changes to the network signal strength (cellular).對網絡信號強度變化監聽*/
PhoneStateListener.this.onSignalStrengthChanged(msg.arg1);
break;
case LISTEN_MESSAGE_WAITING_INDICATOR:/*Listen for changes to the message-waiting indicator.對消息等待指示的變化監聽*/
PhoneStateListener.this.onMessageWaitingIndicatorChanged(msg.arg1 != 0);
break;
case LISTEN_CALL_FORWARDING_INDICATOR:/*Listen for changes to the call-forwarding indicator.對通話轉移指示的變化監聽*/
PhoneStateListener.this.onCallForwardingIndicatorChanged(msg.arg1 != 0);
break;
case LISTEN_CELL_LOCATION:/*Listen for changes to the device's cell location. Note that this will result in frequent callbacks to the listener.對設備單元位置的變化監聽,這會導致頻繁的監聽回調。*/
PhoneStateListener.this.onCellLocationChanged((CellLocation)msg.obj);
break;
case LISTEN_CALL_STATE:/*Listen for changes to the device call state.對設備呼叫狀態的變化監聽。*/
PhoneStateListener.this.onCallStateChanged(msg.arg1, (String)msg.obj);
break;
case LISTEN_DATA_CONNECTION_STATE:/*Listen for changes to the data connection state (cellular).對數據連接狀態的變化監聽。*/
PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1, msg.arg2);
PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1);
break;
case LISTEN_DATA_ACTIVITY:/*Listen for changes to the direction of data traffic on the data connection (cellular).對數據流量移動方向的變化監聽*/
PhoneStateListener.this.onDataActivity(msg.arg1);
break;
case LISTEN_SIGNAL_STRENGTHS:/*Listen for changes to the network signal strengths (cellular).對網絡信號強度的變化監聽*/
PhoneStateListener.this.onSignalStrengthsChanged((SignalStrength)msg.obj);
break;
}
}
};
3.監聽變化后發送消息
IPhoneStateListener callback = new IPhoneStateListener.Stub() {
public void onServiceStateChanged(ServiceState serviceState) {
Message.obtain(mHandler, LISTEN_SERVICE_STATE, 0, 0, serviceState).sendToTarget();
}
public void onSignalStrengthChanged(int asu) {
Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTH, asu, 0, null).sendToTarget();
}
public void onMessageWaitingIndicatorChanged(boolean mwi) {
Message.obtain(mHandler, LISTEN_MESSAGE_WAITING_INDICATOR, mwi ? 1 : 0, 0, null)
.sendToTarget();
}
public void onCallForwardingIndicatorChanged(boolean cfi) {
Message.obtain(mHandler, LISTEN_CALL_FORWARDING_INDICATOR, cfi ? 1 : 0, 0, null)
.sendToTarget();
}
public void onCellLocationChanged(Bundle bundle) {
CellLocation location = CellLocation.newFromBundle(bundle);
Message.obtain(mHandler, LISTEN_CELL_LOCATION, 0, 0, location).sendToTarget();
}
public void onCallStateChanged(int state, String incomingNumber) {
Message.obtain(mHandler, LISTEN_CALL_STATE, state, 0, incomingNumber).sendToTarget();
}
public void onDataConnectionStateChanged(int state, int networkType) {
Message.obtain(mHandler, LISTEN_DATA_CONNECTION_STATE, state, networkType, null).
sendToTarget();
}
public void onDataActivity(int direction) {
Message.obtain(mHandler, LISTEN_DATA_ACTIVITY, direction, 0, null).sendToTarget();
}
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTHS, 0, 0, signalStrength).sendToTarget();
}
};