title: android: background phone state monitoring
body: <p>I'm trying to create a background process which monitors the phone state and displays a toast message if the state is changed. so far, ive implemented these, but when i install and run. It does nothing. It wont display anything. Im posting my codes here. pls help me out.</p>
  
  <p>StateMonitor.java</p>
  
  <p>`</p>
  
  <pre><code>import android.content.Context;
  import android.telephony.PhoneStateListener;
  import android.telephony.TelephonyManager;
  import android.widget.Toast;
  
  public class PhoneStateMonitor extends PhoneStateListener {
      Context context;
  
      public PhoneStateMonitor(Context context) {
          super();
          // TODO Auto-generated constructor stub
          this.context=context;
      }
  
      //This Method Automatically called when changes is detected in Phone State
      public void onCallStateChanged(int state, String incomingNumber) {
          // TODO Auto-generated method stub
          super.onCallStateChanged(state, incomingNumber);
  
          Toast.makeText(context, "Phone State - "+state+" Incoming Number - "+incomingNumber, Toast.LENGTH_LONG).show();//Giving the Message that Phone State Changed
          //Checking The phone state  
          switch(state)
          {
          case TelephonyManager.CALL_STATE_IDLE:    //Phone is in Idle State
              Toast.makeText(context, "Phone State is IDLE", Toast.LENGTH_LONG).show();
              break;
          case TelephonyManager.CALL_STATE_RINGING:  //Phone is Ringing
              Toast.makeText(context, "Phone State is RINGING", Toast.LENGTH_LONG).show();
              break;
          case TelephonyManager.CALL_STATE_OFFHOOK:  //Call is Received
              Toast.makeText(context, "Call State is OFFHOOK",Toast.LENGTH_LONG).show();
              break;
          }
      }
  }
  `
  </code></pre>
  
  <p>PhoneReceiver.java</p>
  
  <pre><code>`import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.telephony.PhoneStateListener;
  import android.telephony.TelephonyManager;
  //Automatically called when State Change is Detected because this Receiver is Registered for PHONE_STATE intent filter in AndroidManifest.xml
  public class PhoneStateReceiver extends BroadcastReceiver {
  
      TelephonyManager manager;       
      PhoneStateMonitor phoneStateListener;
      static boolean isAlreadyListening=false;
  
      //This Method automatically Executed when Phone State Change is Detected
      public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
          phoneStateListener =new PhoneStateMonitor(context);//Creating the Object of Listener
          manager=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);//Getting the Telephony Service Object
          if(!isAlreadyListening)//Checking Listener is Not Registered with Telephony Services
          {
                manager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);//Registering the Listener with Telephony to listen the State Change
            isAlreadyListening=true;  //setting true to indicate that Listener is listening the Phone State
          }
  
      }
  
  }
  `
  </code></pre>
  
  <p>Android Manifest.xml</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.pavan.phonecall"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="10"
          android:targetSdkVersion="21" /&gt;
  
  &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;&lt;!-- Permission for Reading Phone State --&gt;
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
  
          &lt;!--  If this app is not running then this will be automatically called when phone state change detected(Look at Intent Filter) --&gt;
          &lt;receiver android:name=".PhoneStateReceiver"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.PHONE_STATE"/&gt;
              &lt;/intent-filter&gt;
              &lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;`
  </code></pre>
  
  <p>When i install from eclipse , it says apk installed. When i call my phone, Toast message doesnt pop up. where am i going wrong? please help! thanks!</p>