telephonyManager.listen 强制应用程序终止

Posted

技术标签:

【中文标题】telephonyManager.listen 强制应用程序终止【英文标题】:telephonyManager.listen forces application to terminate 【发布时间】:2011-04-20 10:39:00 【问题描述】:

我是 android 新手,正在编写一个用于跟踪呼叫事件的小应用程序。每次我尝试绑定侦听器时,操作系统都会强制应用程序意外关闭。我错过了什么?这是我的代码:

包 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 文本输出; 电话管理器电话管理器; PhoneStateListener phoneStateListener;

@覆盖 公共无效 onDestroy() telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);

/** 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); 


【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于telephonyManager.listen 强制应用程序终止的主要内容,如果未能解决你的问题,请参考以下文章

强连通分量总结

❤️数据结构入门❤️(3 - 1)- 有向图强连通

图之强连通强连通图强连通分量 Tarjan算法

图之强连通强连通图强连通分量 Tarjan算法

总结强连通分量

tarjan有向图的强连通