Browse Source

Added constructors for activity and textView in ThreadDemo

lm
Leon Market 1 year ago
parent
commit
6c4905d0e7

+ 1
- 1
app/src/main/AndroidManifest.xml View File

@@ -12,7 +12,7 @@
android:theme="@style/Theme.Ueberwachungssystem"
tools:targetApi="31">
<activity
android:name=".Beschleunigungssensor"
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

+ 0
- 5
app/src/main/java/com/example/ueberwachungssystem/Beschleunigungssensor.java View File

@@ -28,11 +28,6 @@ public class Beschleunigungssensor extends AppCompatActivity implements SensorEv
private SensorManager sensorManager;
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION;
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;
//Preallocate memory for the data of each axis of the acceleration sensor
double x;

+ 11
- 2
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

@@ -4,6 +4,7 @@ import static java.lang.Boolean.TRUE;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
@@ -12,10 +13,12 @@ import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Logger logger;
private Context context;
//private Accelerometer beschleunigungssensor;
//private SensorManager sensorManager;
private ThreadDemo threadDemo;
private TextView textViewLog;
private TextView textViewWorkerThread;

//private WorkerUsingThread workerUsingThread;
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.setOnClickListener(this);
textViewWorkerThread = (TextView) findViewById(R.id.textViewWorkerThread); //TextView um Thread zu überwachen

// beschleunigungssensor = new Accelerometer(this);
threadDemo = new ThreadDemo(this, textViewWorkerThread);
logger.log("onCreate");
}

@@ -44,6 +48,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
protected void onPause() {
super.onPause();
//beschleunigungssensor.stop();
threadDemo.stop();

}


@@ -52,8 +58,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
logger.log("toggleButton1 is clicked");
if (toggleButton1.isChecked()) {
logger.log("ToggleButton is ON");
//beschleunigungssensor.start();
threadDemo.start();
} else {
logger.log("ToggleButton is OFF");
threadDemo.stop();
}
}
}

+ 10
- 9
app/src/main/java/com/example/ueberwachungssystem/ThreadDemo.java View File

@@ -9,20 +9,21 @@ public class ThreadDemo implements Runnable {
private volatile boolean running = false;
private Thread thread;
private String threadname = "testThread";
Logger logger;
//Logger logger;
TextView textViewWorkerThread;

// Passing Activity's instance as argument on worker thread
AppCompatActivity activity;
public ThreadDemo(AppCompatActivity activity){
public ThreadDemo(AppCompatActivity activity, TextView textViewWorkerThread){
this.activity = activity;
this.textViewWorkerThread = textViewWorkerThread;
}

//Method print which delegates access on MainActivity to runOnUiThread
private void print(final String s) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
//textViewWorkerThread.setText(s);
textViewWorkerThread.setText(s);
}
});
}
@@ -43,23 +44,23 @@ public class ThreadDemo implements Runnable {
}

void start() {
logger.log("Starting " + threadname + "...");
//logger.log("Starting " + threadname + "...");
running = true;
thread = new Thread(this);
thread.setName(threadname);
thread.start();
logger.log("..." + threadname + " started");
//logger.log("..." + threadname + " started");
}
void stop() {
if (!running) {
logger.log(threadname + " not running");
//logger.log(threadname + " not running");
} else {
logger.log("Stopping " + threadname + "...");
//logger.log("Stopping " + threadname + "...");
running = false;
while(true){
try {
thread.join();
logger.log("... " + threadname + " stopped");
//logger.log("... " + threadname + " stopped");
break;
} catch (InterruptedException e) {
e.printStackTrace();

+ 2
- 2
app/src/main/res/layout/activity_main.xml View File

@@ -10,7 +10,7 @@
android:id="@+id/textViewLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="TextViewLoggerMain"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
@@ -35,7 +35,7 @@
android:layout_marginStart="174dp"
android:layout_marginTop="85dp"
android:layout_marginEnd="179dp"
android:text="TextView"
android:text="textViewWorkerThreadTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toggleButton1" />

Loading…
Cancel
Save