Using sensor value inBroadcastReceiver

139 views Asked by At

I have implemented a BroadcastReceiver to get the incoming call state using below code

public class callReceiver extends BroadcastReceiver {
    Context mContext;

    @Override
    public void onReceive(Context mContext, Intent intent)
    {

           String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                Toast.makeText(mContext, "Phone Is Ringing", Toast.LENGTH_LONG).show();
               // Your Code
           }
    }
}

This states the incoming call but now i want to use proximity sensor to get value and pick call while sensor value changed i can use Activity implementing SensorEventListener but how to use these value in broad casr receiver.

1

There are 1 answers

0
Ankur Kumar On

Broadcast receiver doesnt allow processing for more than 5 seconds and also the context is destroyed once the execution for onReceive method is over. You can actually have a receiver for proximity sensor and check telephony manager for the call state inside the onReceive method.

Or you can start a service in the above receiver and register for proximity sensor there.