我是使用Android Studio的新手,目前正在编写一个应用程序,该应用程序使用手机中的传感器来控制其他设备。我搜索了,但没有找到太多可以帮助我解决我的问题。最终我需要通过网络传输从传感器获得的数据,但我还没有接近那个点,所以这是另一天。Android Studio - 使用按钮开始和停止循环按
此时,应用程序可以访问传感器并将其显示在手机屏幕上。每次按下屏幕上的按钮,它都会更新读数。我想要做的事情是,只要按一次按钮,随着手机的移动,不断更新实时值。如果再次按下该按钮,我希望它停止。以下是我目前在主要活动中的代码。我试图做的是使用一个整数,每次按下按钮时都会切换,并在其中一个值上运行do while循环。当while循环在那里时,它不会做任何事情。如果我把它拿出来,就像它每次按下按钮时更新值一样运行。我还在传感器值旁边显示“切换”的值,并在do while循环不在时切换。我不明白为什么while循环不会运行。我也尝试过使用布尔值并在true和false之间切换,但我得到了相同的结果。我也意识到do while循环的设置方式可能无法停止,但我会认为它至少会进入循环并保持运行,至少让我开始运行。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity
{
MySensorUpdateThread mySensorUpdateThread = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySensorUpdateThread = new MySensorUpdateThread(this);
SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
final float[] mValuesMagnet = new float[3];
final float[] mValuesAccel = new float[3];
final float[] mValuesOrientation = new float[3];
final float[] mRotationMatrix = new float[9];
final Button btn_valider = (Button) findViewById(R.id.btn1);
final TextView txt1 = (TextView) findViewById(R.id.textView1);
final SensorEventListener mEventListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
break;
}
}
;
};
setListners(sensorManager, mEventListener);
btn_valider.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
mySensorUpdateThread.toggleThread();
if (mySensorUpdateThread.isRunning())
{
mySensorUpdateThread.start();
}
}
});
}
public void setListners(SensorManager sensorManager, SensorEventListener mEventListener)
{
sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL);
}
public class MySensorUpdateThread extends Thread
{
private boolean keepRunning = false;
private String sensorResults = "";
private MainActivity mActivity;
public MySensorUpdateThread(MainActivity activity)
{
this.mActivity = activity;
}
public void toggleThread()
{
this.keepRunning = !this.keepRunning;
}
public boolean isRunning()
{
return this.keepRunning;
}
public String getSensorResults()
{
return this.sensorResults;
}
@Override
public void run()
{
int i = 0;
int maxIterations = 100;
try{
while(this.keepRunning)
{
// This keeps the thread from going on too long in case
if(i > maxIterations)
{
this.keepRunning = false;
break;
}
// This causes the thread to rest for 50ms to
// slow things down
try
{
Thread.sleep(50);
}
catch(InterruptedException e)
{
}
SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
sensorResults = "Roll/Pitch (degrees): " + /*mValuesOrientation[0]*(180/Math.PI) + " "+ "," + " " +*/
mValuesOrientation[1] * (180/Math.PI) + " " + "/" + " " +
mValuesOrientation[2] * (-180/Math.PI);
// Now post the results to the UI Thread
runOnUiThread(new Runnable(){
@Override
public void run(){
txt1.setText(getSensorResults());
}
});
}
}
catch()
{
Log.e(TAG, ex.getMessage());
}
}
}
}
您是否收到任何错误? – Barns
我认为你的问题不在于循环本身,而在于你如何使用它。我想你在等待结果的同时不停地做,是吗?并且在运行for循环时,您是否遇到了用户界面中的冻结时刻,并且字面上感觉没有任何反应? – jace
杰斯是对的。你在循环中阻止你的用户界面。你需要在后台线程中运行循环。 – Barns