我是使用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