posts - 93,  comments - 2,  trackbacks - 0

          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();
              }
          };

          posted on 2013-05-18 09:38 Terry Zou 閱讀(352) 評論(0)  編輯  收藏 所屬分類: Android
          <2013年5月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 湘阴县| 建昌县| 西乌珠穆沁旗| 德江县| 新安县| 吴川市| 怀仁县| 五河县| 蓬安县| 吉林省| 五大连池市| 临武县| 仁化县| 保亭| 平邑县| 南康市| 开封市| 通州市| 松原市| 克拉玛依市| 南开区| 浮山县| 阿拉尔市| 墨玉县| 贵港市| 泰宁县| 台湾省| 绥宁县| 阜平县| 安康市| 伊川县| 呼图壁县| 磐安县| 宜宾县| 壶关县| 三河市| 宜春市| 黄大仙区| 大田县| 固始县| 马尔康县|