android:theme="@style/Theme.Ueberwachungssystem" | android:theme="@style/Theme.Ueberwachungssystem" | ||||
tools:targetApi="31"> | tools:targetApi="31"> | ||||
<activity | <activity | ||||
android:name=".Beschleunigungssensor" | |||||
android:name=".MainActivity" | |||||
android:exported="true"> | android:exported="true"> | ||||
<intent-filter> | <intent-filter> | ||||
<action android:name="android.intent.action.MAIN" /> | <action android:name="android.intent.action.MAIN" /> |
private SensorManager sensorManager; | private SensorManager sensorManager; | ||||
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION; | private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION; | ||||
private Sensor sensor; | private Sensor sensor; | ||||
//Matrizen für Datenverarbeitung: | |||||
double[] linear_acceleration = new double[3]; | |||||
int numberOfValues = 100; | |||||
double[][] calibrationMatrix = new double[3][numberOfValues]; | |||||
boolean alarm = false; | boolean alarm = false; | ||||
//Preallocate memory for the data of each axis of the acceleration sensor | //Preallocate memory for the data of each axis of the acceleration sensor | ||||
double x; | double x; |
import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||
import android.content.Context; | |||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.view.View; | import android.view.View; | ||||
import android.widget.TextView; | import android.widget.TextView; | ||||
public class MainActivity extends AppCompatActivity implements View.OnClickListener { | public class MainActivity extends AppCompatActivity implements View.OnClickListener { | ||||
private Logger logger; | private Logger logger; | ||||
private Context context; | |||||
//private Accelerometer beschleunigungssensor; | //private Accelerometer beschleunigungssensor; | ||||
//private SensorManager sensorManager; | |||||
private ThreadDemo threadDemo; | |||||
private TextView textViewLog; | private TextView textViewLog; | ||||
private TextView textViewWorkerThread; | private TextView textViewWorkerThread; | ||||
//private WorkerUsingThread workerUsingThread; | //private WorkerUsingThread workerUsingThread; | ||||
ToggleButton toggleButton1; | ToggleButton toggleButton1; | ||||
toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1); //togglebutton um Thread zu steuern | toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1); //togglebutton um Thread zu steuern | ||||
toggleButton1.setOnClickListener(this); | toggleButton1.setOnClickListener(this); | ||||
textViewWorkerThread = (TextView) findViewById(R.id.textViewWorkerThread); //TextView um Thread zu überwachen | textViewWorkerThread = (TextView) findViewById(R.id.textViewWorkerThread); //TextView um Thread zu überwachen | ||||
// beschleunigungssensor = new Accelerometer(this); | |||||
threadDemo = new ThreadDemo(this, textViewWorkerThread); | |||||
logger.log("onCreate"); | logger.log("onCreate"); | ||||
} | } | ||||
protected void onPause() { | protected void onPause() { | ||||
super.onPause(); | super.onPause(); | ||||
//beschleunigungssensor.stop(); | //beschleunigungssensor.stop(); | ||||
threadDemo.stop(); | |||||
} | } | ||||
logger.log("toggleButton1 is clicked"); | logger.log("toggleButton1 is clicked"); | ||||
if (toggleButton1.isChecked()) { | if (toggleButton1.isChecked()) { | ||||
logger.log("ToggleButton is ON"); | logger.log("ToggleButton is ON"); | ||||
//beschleunigungssensor.start(); | |||||
threadDemo.start(); | |||||
} else { | } else { | ||||
logger.log("ToggleButton is OFF"); | logger.log("ToggleButton is OFF"); | ||||
threadDemo.stop(); | |||||
} | } | ||||
} | } | ||||
} | } |
private volatile boolean running = false; | private volatile boolean running = false; | ||||
private Thread thread; | private Thread thread; | ||||
private String threadname = "testThread"; | private String threadname = "testThread"; | ||||
Logger logger; | |||||
//Logger logger; | |||||
TextView textViewWorkerThread; | |||||
// Passing Activity's instance as argument on worker thread | // Passing Activity's instance as argument on worker thread | ||||
AppCompatActivity activity; | AppCompatActivity activity; | ||||
public ThreadDemo(AppCompatActivity activity){ | |||||
public ThreadDemo(AppCompatActivity activity, TextView textViewWorkerThread){ | |||||
this.activity = activity; | this.activity = activity; | ||||
this.textViewWorkerThread = textViewWorkerThread; | |||||
} | } | ||||
//Method print which delegates access on MainActivity to runOnUiThread | //Method print which delegates access on MainActivity to runOnUiThread | ||||
private void print(final String s) { | private void print(final String s) { | ||||
activity.runOnUiThread(new Runnable() { | activity.runOnUiThread(new Runnable() { | ||||
@Override | @Override | ||||
public void run() { | public void run() { | ||||
//textViewWorkerThread.setText(s); | |||||
textViewWorkerThread.setText(s); | |||||
} | } | ||||
}); | }); | ||||
} | } | ||||
} | } | ||||
void start() { | void start() { | ||||
logger.log("Starting " + threadname + "..."); | |||||
//logger.log("Starting " + threadname + "..."); | |||||
running = true; | running = true; | ||||
thread = new Thread(this); | thread = new Thread(this); | ||||
thread.setName(threadname); | thread.setName(threadname); | ||||
thread.start(); | thread.start(); | ||||
logger.log("..." + threadname + " started"); | |||||
//logger.log("..." + threadname + " started"); | |||||
} | } | ||||
void stop() { | void stop() { | ||||
if (!running) { | if (!running) { | ||||
logger.log(threadname + " not running"); | |||||
//logger.log(threadname + " not running"); | |||||
} else { | } else { | ||||
logger.log("Stopping " + threadname + "..."); | |||||
//logger.log("Stopping " + threadname + "..."); | |||||
running = false; | running = false; | ||||
while(true){ | while(true){ | ||||
try { | try { | ||||
thread.join(); | thread.join(); | ||||
logger.log("... " + threadname + " stopped"); | |||||
//logger.log("... " + threadname + " stopped"); | |||||
break; | break; | ||||
} catch (InterruptedException e) { | } catch (InterruptedException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); |
android:id="@+id/textViewLog" | android:id="@+id/textViewLog" | ||||
android:layout_width="wrap_content" | android:layout_width="wrap_content" | ||||
android:layout_height="wrap_content" | android:layout_height="wrap_content" | ||||
android:text="Hello World!" | |||||
android:text="TextViewLoggerMain" | |||||
app:layout_constraintBottom_toBottomOf="parent" | app:layout_constraintBottom_toBottomOf="parent" | ||||
app:layout_constraintEnd_toEndOf="parent" | app:layout_constraintEnd_toEndOf="parent" | ||||
app:layout_constraintStart_toStartOf="parent" | app:layout_constraintStart_toStartOf="parent" | ||||
android:layout_marginStart="174dp" | android:layout_marginStart="174dp" | ||||
android:layout_marginTop="85dp" | android:layout_marginTop="85dp" | ||||
android:layout_marginEnd="179dp" | android:layout_marginEnd="179dp" | ||||
android:text="TextView" | |||||
android:text="textViewWorkerThreadTextView" | |||||
app:layout_constraintEnd_toEndOf="parent" | app:layout_constraintEnd_toEndOf="parent" | ||||
app:layout_constraintStart_toStartOf="parent" | app:layout_constraintStart_toStartOf="parent" | ||||
app:layout_constraintTop_toBottomOf="@+id/toggleButton1" /> | app:layout_constraintTop_toBottomOf="@+id/toggleButton1" /> |