1

我想开发一个 android 应用程序,我想使用接近传感器或加速度计传感器接听电话。如果有来电,当我得到一定的 sensor 值时,就会自动接听来电。例如,当我将手在接近传感器前从左到右移动时,将接听电话,从右到左,则电话将被拒绝。在加速度计的情况下,当我用耳朵触摸我的手机时,传感器会收到一定的 x,y,z 值,然后就会接到电话。我尝试了一些方法,但是当手机接到一个号码的电话时,我无法读取传感器值。手机接听电话时是否可以在 BroadCastReceiver 中获取传感器值或读取传感器?我已经从名为“
https://play.google.com/store/apps/details?id=com.MarksThinkTank.WaveControlPro&feature=search_result

4

1 回答 1

1

像这样使用这里我让传感器水平和垂直

public class SensorActivity extends Activity implements SensorEventListener{
    private static final String TAG="SensorActivity";

    private Context context=null;
    private float mLastX, mLastY, mLastZ;
    private boolean mInitialized;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private final float NOISE = (float) 5.0;

    ArrayList<String> arraylist=null;

    private SharedPreferences seedPrefs = null;
    private String prefname = "shakingvideo";

    VideoBean vbean= new VideoBean();

      @Override
      public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.video);
           context=this;

           mInitialized = false;
           mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
           mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
           mSensorManager.registerListener(this, mAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);

            arraylist= new ArrayList<String>();

      }

      @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

      @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }


    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    public void onSensorChanged(SensorEvent event) {

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];


        if (!mInitialized) {
            mLastX = x;
            mLastY = y;
            mLastZ = z;
            mInitialized = true;
        } else {
            float deltaX = Math.abs(mLastX - x);
            float deltaY = Math.abs(mLastY - y);
            float deltaZ = Math.abs(mLastZ - z);
            if (deltaX < NOISE) deltaX = (float)0.0;
            if (deltaY < NOISE) deltaY = (float)0.0;
            if (deltaZ < NOISE) deltaZ = (float)0.0;
            mLastX = x;
            mLastY = y;
            mLastZ = z;
            if (deltaX > deltaY) {


                // horizontal shaking    receive here your call

            } else if (deltaY > deltaX) {

                //Reject here your call

            } else {
                // no shaking
            }
        }
    }
}
于 2013-06-11T05:16:31.617 回答