来自应用程序类中的传感器侦听器的意图

时间:2013-03-14 12:50:53

标签: android android-intent

我正在尝试在我的应用中加入摇晃功能。为此,无论用户在应用程序中的哪个位置,我都要求在手机抖动时进行活动。现在我将我的传感器代码放在应用程序类中,但为了实现它需要上下文。我是正确的方式还是有其他方法来实现这一功能? 这是我的代码

public class MyApplication extends Application {


/* variables for shake detection */
private SensorManager mSensorManager;
private float mAccel; // acceleration apart from gravity
private float mAccelCurrent; // current acceleration including gravity
private float mAccelLast; // last acceleration including gravity
@Override
public void onCreate(){

    /* sensor shake detection */
  mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    mAccel = 0.00f;
    mAccelCurrent = SensorManager.GRAVITY_EARTH;
    mAccelLast = SensorManager.GRAVITY_EARTH;   
}


private final SensorEventListener mSensorListener = new SensorEventListener() {

    public void onSensorChanged(SensorEvent se) {
      float x = se.values[0];
      float y = se.values[1];
      float z = se.values[2]; 
      mAccelLast = mAccelCurrent;
      mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
      float delta = mAccelCurrent - mAccelLast;
      mAccel = mAccel * 0.9f + delta; // perform low-cut filter

      if(mAccel > 3.0f)
      {
         // Toast.makeText(getBaseContext(), "Phone shaked", Toast.LENGTH_SHORT).show();
          Intent intent = new Intent(getApplicationContext(),LiveSearchActivity.class);
          startActivity(intent);


      }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

  };

}

2 个答案:

答案 0 :(得分:0)

您的课程正在扩展应用程序,而不是您应该扩展活动。

public class MyApplication extends Activity
{
    @Override
    protected void onCreate(Bundle Sumit)
    { 
        super.onCreate(Sumit);
    }    
}

答案 1 :(得分:0)

尝试使用此代码。我检查了一下。有效 。 现在正确的是,它将重定向到移动设备震动后你想去的地方。希望它会对你有所帮助。

package com.example.stack;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class MyApplication extends Application {


/* variables for shake detection */
private SensorManager mSensorManager;
private float mAccel; // acceleration apart from gravity
private float mAccelCurrent; // current acceleration including gravity
private float mAccelLast; // last acceleration including gravity
Context context;
@Override
public void onCreate(){

    context=this;
    /* sensor shake detection */
  mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    mAccel = 0.00f;
    mAccelCurrent = SensorManager.GRAVITY_EARTH;
    mAccelLast = SensorManager.GRAVITY_EARTH;   
}


private final SensorEventListener mSensorListener = new SensorEventListener() {

    public void onSensorChanged(SensorEvent se) {
      float x = se.values[0];
      float y = se.values[1];
      float z = se.values[2]; 
      mAccelLast = mAccelCurrent;
      mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
      float delta = mAccelCurrent - mAccelLast;
      mAccel = mAccel * 0.9f + delta; // perform low-cut filter

      if(mAccel > 3.0f)
      {
         // Toast.makeText(getBaseContext(), "Phone shaked", Toast.LENGTH_SHORT).show();
          Intent intent = new Intent(context,MainActivity.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       

          startActivity(intent);


      }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

  };

}