首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >telephonyManager.listen强制应用程序终止

telephonyManager.listen强制应用程序终止
EN

Stack Overflow用户
提问于 2010-10-03 18:05:13
回答 1查看 2.8K关注 0票数 1

我是个Android新手,正在写一个跟踪通话事件的小应用程序。每次我尝试绑定listner时,操作系统都会强制应用程序不加注意地关闭。我错过了什么?下面是我的代码:

打包com.example.helloandroid;

导入android.app.Activity;导入android.content.Context;导入android.os.Bundle;导入android.telephony.CellLocation;导入android.telephony.PhoneStateListener;导入android.telephony.ServiceState;导入android.telephony.TelephonyManager;导入android.widget.TextView;

公共类helloAndroid扩展了Activity {

TextView textOut;TelephonyManager telephonyManager;PhoneStateListener phoneStateListener;

@Override public void onDestroy(){ telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_NONE);

}

代码语言:javascript
复制
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 // Get the UI
    textOut = (TextView) findViewById(R.id.textOut);

 // Get the telephony manager
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    // Create a new PhoneStateListener
    phoneStateListener = new PhoneStateListener() {
     public void onCallForwardingIndicatorChanged(boolean cfi) {}
     public void onCallStateChanged(int state, String incomingNumber) {}
     public void onCellLocationChanged(CellLocation location) {}
     public void onDataActivity(int direction) {}
     public void onDataConnectionStateChanged(int state) {}
     public void onMessageWaitingIndicatorChanged(boolean mwi) {}
     public void onServiceStateChanged(ServiceState serviceState) {

      String stateString = "N/A";
         switch (serviceState.getState()) {
         case TelephonyManager.CALL_STATE_IDLE:
           stateString = "Idle";
           break;
         case TelephonyManager.CALL_STATE_OFFHOOK:
           stateString = "Off Hook";
           break;
         case TelephonyManager.CALL_STATE_RINGING:
           stateString = "Ringing";
           break;
         }
         textOut.append(String.format("\nonCallStateChanged: %s", stateString));

     }
     public void onSignalStrengthChanged(int asu) {}
     };

    // Register the listener with the telephony manager
    telephonyManager.listen(phoneStateListener,
       PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
       PhoneStateListener.LISTEN_CALL_STATE |
       PhoneStateListener.LISTEN_CELL_LOCATION |
       PhoneStateListener.LISTEN_DATA_ACTIVITY |
       PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
       PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR |
       PhoneStateListener.LISTEN_SERVICE_STATE |
       PhoneStateListener.LISTEN_SIGNAL_STRENGTH); 

}

}

EN

回答 1

Stack Overflow用户

发布于 2010-10-03 18:26:55

我的错误:需要添加权限ACCESS_COARSE_LOCATION

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3849320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档