title: Android READ PHONE STATE?
body: <p>I'm trying to make app to <code>READ PHONE STATE</code> and when the phone state is changed to display <code>Toast</code> with the current state. But when I start it, the app stops unexpectedly.</p>
  
  <p>my class :</p>
  
  <pre><code>import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.telephony.PhoneStateListener;
  import android.telephony.TelephonyManager;
  import android.widget.TextView;
  
  public class TelephonyDemo extends Activity {
      TextView textOut;
      TelephonyManager telephonyManager;
      PhoneStateListener listener;
  
      /** 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
          listener = new PhoneStateListener() {
              @Override
              public void onCallStateChanged(int state, String incomingNumber) {
                  String stateString = "N/A";
                  switch (state) {
                  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));
              }
          };
  
          // Register the listener with the telephony manager
          telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
      }
  }
  </code></pre>
  
  <p>my manifest is :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.marakana"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;application
          android:icon="@drawable/icon"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.Light" &gt;
          &lt;activity
              android:name=".TelephonyDemo"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
      &lt;uses-sdk android:minSdkVersion="7" /&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>My layout is :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Telephony Demo"
          android:textSize="22sp" /&gt;
  
      &lt;TextView
          android:id="@+id/textOut"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Output" &gt;
      &lt;/TextView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>