为您的应用添加抖动识别的最佳方式(Android)

时间:2016-05-30 21:28:43

标签: android android-intent android-activity toast android-sensors

我的英语不是很好 - 所以如果我的问题听起来有些混乱,我很抱歉;)

我做了一项识别强势动作的活动。如果我将活动作为应用程序中的单个活动启动,它确实有效。但现在,我想将活动添加到其他应用程序。我试图将活动作为第二项活动开始。似乎不可能同时运行这两个活动!?我不想将第二个活动的功能复制到我的主要活动中 - 保持主要活动的清洁和功能封装。请告诉我将我的功能(运动识别与祝酒)添加到我的应用程序的最佳方式。

所以......这是我想要从一项活动改为......无论如何......我想要的代码......

public class SensorActivity extends Activity
    implements SensorEventListener{

//static SensorActivity sensorActivity;

// ToDo: test and ACCELMAX for Smartphones and Glasses
// Glasses should have a lower value
private static final int ACCELMAX = 15;

private SensorManager sensorMan;
private Sensor accelerometer;

private float[] mGravity;
private float mAccel;
private float mAccelCurrent;
private float mAccelLast;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //sensorActivity = this;

    sensorMan=(SensorManager)

            getSystemService(SENSOR_SERVICE);

    accelerometer=sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mAccel=0.00f;
    mAccelCurrent=SensorManager.GRAVITY_EARTH;
    mAccelLast=SensorManager.GRAVITY_EARTH;
}

// for closing activity in an other activity
/*
public static SensorActivity getInstance(){
    return sensorActivity;
}*/

@Override
public void onResume() {
    super.onResume();
    sensorMan.registerListener(this, accelerometer,
            SensorManager.SENSOR_DELAY_UI);
}

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

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        mGravity = event.values.clone();
        // Shake detection
        float x = mGravity[0];
        float y = mGravity[1];
        float z = mGravity[2];
        mAccelLast = mAccelCurrent;
        mAccelCurrent = (float)Math.sqrt(x * x + y * y + z * z);
        float delta = mAccelCurrent - mAccelLast;
        mAccel = mAccel * 0.9f + delta;
        if (mAccel > ACCELMAX) {

            // custom toast warning
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.warning_toast, null);

            ImageView imgToast = (ImageView) layout.findViewById(R.id.toastImage);
            //imgToast.setImageResource(R.drawable.dontrun);

            TextView text = (TextView) layout.findViewById(R.id.toastText);
            text.setText("");   // the warning is in the background, so we don't need text atm

            Toast dontRunToast = Toast.makeText(getApplication(), "", Toast.LENGTH_SHORT);
            dontRunToast.setView(layout);
            dontRunToast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
            dontRunToast.show();
        }
    }

}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // required method
}

}

1 个答案:

答案 0 :(得分:0)

您无法在this life sycle

中看到的同时开展两项活动

我认为您必须将这些数学代码复制到主要活动才能使用它们。 为了保持你的活动整洁有序,你可以使用(适用于android studio,我现在不用关于eclipse

//region *title*

*write your methode here*

//endregion

这些注释可以让您关闭它们之间的代码并使其最小化 like that