Added constructors for activity and textView in ThreadDemo
This commit is contained in:
parent
402b73e4bd
commit
6c4905d0e7
@ -12,7 +12,7 @@
|
|||||||
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" />
|
||||||
|
@ -28,11 +28,6 @@ public class Beschleunigungssensor extends AppCompatActivity implements SensorEv
|
|||||||
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;
|
||||||
|
@ -4,6 +4,7 @@ import static java.lang.Boolean.TRUE;
|
|||||||
|
|
||||||
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;
|
||||||
@ -12,10 +13,12 @@ import android.widget.ToggleButton;
|
|||||||
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;
|
||||||
|
|
||||||
@ -32,7 +35,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|||||||
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,6 +48,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
//beschleunigungssensor.stop();
|
//beschleunigungssensor.stop();
|
||||||
|
threadDemo.stop();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -52,8 +58,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,20 +9,21 @@ public class ThreadDemo implements Runnable {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -43,23 +44,23 @@ public class ThreadDemo implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
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"
|
||||||
@ -35,7 +35,7 @@
|
|||||||
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" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user