title: BroadCastReceiver to listen incoming call and work between time interval
body: <p><em>I want to develope a receiver class which will listen Phone state. And I want to work on doSomething() using incoming phone number.</em></p>
  
  <p><em>And my need is that my receiver should work between some time interval (like 12:30PM to 01:00PM), other than this time interval my receiver should not work.</em></p>
  
  <p><strong>What I did yet :</strong> </p>
  
  <p><em>This is my BroadcastReceiver class :</em> </p>
  
  <pre><code>public class MyTest extends BroadcastReceiver {
      protected AudioManager audioManager;// = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      protected Context context;
      private ITelephony telephonyService;
      @Override
       public void onReceive(Context context, Intent intent) {
        audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        this.context = context;
        String action = intent.getAction();
               if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE")){
                if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                                    TelephonyManager.EXTRA_STATE_RINGING)) {
                    //Incoming call 
                 doSomething(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
                }         
               }
               else {
  
               Bundle bundle = intent.getExtras();
               Object[] pdus = (Object[]) bundle.get("pdus");
               SmsMessage message = SmsMessage.createFromPdu((byte[])pdus[0]);
               if(!message.isEmail())
                   doSomething(message.getOriginatingAddress());
  
               }
  
       }
  }
  </code></pre>
  
  <p><em>And I am using it as in my activity :</em> </p>
  
  <pre><code>registerReceiver(new MyTest(), new IntentFilter("android.intent.action.PHONE_STATE"));
  </code></pre>
  
  <p><em>In manifest I declaired it as</em> </p>
  
  <pre><code>&lt;receiver android:name=".MyTest"&gt;&lt;/receiver&gt;
  </code></pre>
  
  <p><strong>Problem :</strong> </p>
  
  <p>Its working if my application is running on foreground, but when I closes my application its not working. 
  <em>I didn't write any code on onPause() or other methods of Activity life cycle.</em></p>
  
  <p><em><strong>What am I doing wrong? Is there any other way to this?</em></strong> </p>