2010-10-03 153 views
0

Iam与Android新,并写一个跟踪呼叫事件的小应用程序。每次我尝试绑定列表器时,os都会强制应用程序关闭,以至于无法接受。我错过了什么?这里是我的代码:telephonyManager.listen强制应用程序终止

package com.example.helloandroid;

进口android.app.Activity; import android.content.Context; import android.os.Bundle; import android.telephony.CellLocation; import android.telephony.PhoneStateListener; import android.telephony.ServiceState; import android.telephony.TelephonyManager; import android.widget.TextView;

public class helloAndroid extends Activity {

TextView textOut; TelephonyManager telephonyManager; PhoneStateListener phoneStateListener;

@Override public void 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); 

} 

}

+0

如果应用程序意外关闭需要的,什么是你在你的logcat得到错误?我想你可以在那里找到问题的原因。 – mreichelt 2010-10-03 10:11:26

回答

0

我的错误:添加权限ACCESS_COARSE_LOCATION