Android 在路旁接收器中使用传感器值

Android 在路旁接收器中使用传感器值,android,broadcastreceiver,proximitysensor,Android,Broadcastreceiver,Proximitysensor,我使用下面的代码实现了一个BroadcastReceiver来获取传入呼叫状态 public class callReceiver extends BroadcastReceiver { Context mContext; @Override public void onReceive(Context mContext, Intent intent) { String state = intent.getStringExtra(Telep

我使用下面的代码实现了一个BroadcastReceiver来获取传入呼叫状态

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
           }
    }
}
这表示传入呼叫,但现在我想使用接近传感器获取值,并在传感器值更改时拾取呼叫
我可以使用Activity实现SensorEventListener,但如何在广泛的casr接收器中使用这些值。

广播接收器不允许处理超过5秒,并且一旦onReceive方法的执行结束,上下文将被破坏。实际上,您可以为接近传感器配备一个接收器,并在onReceive方法中检查telephony manager的呼叫状态

或者,您可以在上述接收器中启动服务,并在那里注册接近传感器