gpt4 book ai didi

java - 按照创建摇动监听器的教程进行操作,但使用时遇到问题

转载 作者:行者123 更新时间:2023-12-01 12:59:26 26 4
gpt4 key购买 nike

我只是想简单地学习android。我已经按照 this tutorial 中有关如何实现“摇动监听器”的教程进行操作。

就是这样:

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.FloatMath;

public class ShakeListener implements SensorEventListener {

/*
* The gForce that is necessary to register as shake.
* Must be greater than 1G (one earth gravity unit).
* You can install "G-Force", by Blake La Pierre
* from the Google Play Store and run it to see how
* many G's it takes to register a shake
*/
private static final float SHAKE_THRESHOLD_GRAVITY = 2.7F;
private static final int SHAKE_SLOP_TIME_MS = 500;
private static final int SHAKE_COUNT_RESET_TIME_MS = 3000;

private OnShakeListener mListener;
private long mShakeTimestamp;
private int mShakeCount;

public void setOnShakeListener(OnShakeListener listener) {
this.mListener = listener;
}

public interface OnShakeListener {
public void onShake(int count);
}

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

@Override
public void onSensorChanged(SensorEvent event) {

if (mListener != null) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];

float gX = x / SensorManager.GRAVITY_EARTH;
float gY = y / SensorManager.GRAVITY_EARTH;
float gZ = z / SensorManager.GRAVITY_EARTH;

// gForce will be close to 1 when there is no movement.
float gForce = FloatMath.sqrt(gX * gX + gY * gY + gZ * gZ);

if (gForce > SHAKE_THRESHOLD_GRAVITY) {
final long now = System.currentTimeMillis();
// ignore shake events too close to each other (500ms)
if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) {
return;
}

// reset the shake count after 3 seconds of no shakes
if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) {
mShakeCount = 0;
}

mShakeTimestamp = now;
mShakeCount++;

mListener.onShake(mShakeCount);
}
}
}
}

我从文件夹中阅读笑话,并且希望每次摇动手机时都会显示一个新笑话。我不知道如何调用摇动监听器。

private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeListener mShakeListener;

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

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

TextView textView = new TextView(this);
textView.setTextSize(16);
String[] test = processData(readText(message));
textView.setText(test[0]); // later will use random

//________________________________________________
// THIS IS THE PART I HAVE NO IDEA HOW TO IMPLEMENT.
while (true) {
if (shake)
textView.setText(test[RandomNumber]);
}
//________________________________________________

setContentView(textView);
}

我认为这是我应该使用的,但坦率地说,我不明白发生了什么:

// ShakeListener initialization
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mShakeListener = new ShakeListener();
mShakeListener.setOnShakeListener(new OnShakeListener() {

@Override
public void onShake(int count) {
/*
* The following method, "handleShakeEvent(count):" is a stub //
* method you would use to setup whatever you want done once the
* device has been shook.
*/
handleShakeEvent(count);
}
});

最佳答案

你不调用Listeners。他们给你打电话。

移动:

while (true) {
if (shake)
textView.setText(test[RandomNumber]); // this line
}

至:

@Override
public void onShake(int count) {
/*
* HERE
*/
handleShakeEvent(count);
}

应该实现你的目标。

关于java - 按照创建摇动监听器的教程进行操作,但使用时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23637519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com