-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdatedMainActivity.java
More file actions
164 lines (142 loc) · 5.89 KB
/
UpdatedMainActivity.java
File metadata and controls
164 lines (142 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.example.sensor;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
/**
* SensorListeners demonstrates how to gain access to sensors (here, the light
* and proximity sensors), how to register sensor listeners, and how to
* handle sensor events.
*/
public class MainActivity extends AppCompatActivity
implements SensorEventListener {
// System sensor manager instance.
private SensorManager mSensorManager;
// Proximity and light sensors, as retrieved from the sensor manager.
private Sensor mSensorProximity;
private Sensor mSensorLight;
// TextViews to display current sensor values.
private TextView mTextSensorLight;
private TextView mTextSensorProximity;
private Socket client;
private PrintWriter printwriter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize all view variables.
mTextSensorLight = (TextView) findViewById(R.id.label_light);
mTextSensorProximity = (TextView) findViewById(R.id.label_proximity);
// Get an instance of the sensor manager.
mSensorManager = (SensorManager) getSystemService(
Context.SENSOR_SERVICE);
// Get light and proximity sensors from the sensor manager.
// The getDefaultSensor() method returns null if the sensor
// is not available on the device.
mSensorProximity = mSensorManager.getDefaultSensor(
Sensor.TYPE_PROXIMITY);
mSensorLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
// Get the error message from string resources.
String sensor_error = getResources().getString(R.string.error_no_sensor);
// If either mSensorLight or mSensorProximity are null, those sensors
// are not available in the device. Set the text to the error message
if (mSensorLight == null) { mTextSensorLight.setText(sensor_error); }
if (mSensorProximity == null) {
mTextSensorProximity.setText(sensor_error);
}
}
@Override
protected void onStart() {
super.onStart();
// Listeners for the sensors are registered in this callback and
// can be unregistered in onPause().
//
// Check to ensure sensors are available before registering listeners.
// Both listeners are registered with a "normal" amount of delay
// (SENSOR_DELAY_NORMAL)
if (mSensorProximity != null) {
mSensorManager.registerListener(this, mSensorProximity,
SensorManager.SENSOR_DELAY_NORMAL);
}
if (mSensorLight != null) {
mSensorManager.registerListener(this, mSensorLight,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onStop() {
super.onStop();
// Unregister all sensor listeners in this callback so they don't
// continue to use resources when the app is paused.
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
// The sensor type (as defined in the Sensor class).
int sensorType = sensorEvent.sensor.getType();
// The new data value of the sensor. Both the light and proximity
// sensors report one value at a time, which is always the first
// element in the values array.
float currentValue = sensorEvent.values[0];
switch (sensorType) {
// Event came from the light sensor.
case Sensor.TYPE_LIGHT:
// Set the light sensor text view to the light sensor string
// from the resources, with the placeholder filled in.
mTextSensorLight.setText(getResources().getString(
R.string.label_light, currentValue));
new Thread(new ClientThread(getResources().getString(
R.string.label_light, currentValue))).start();
break;
case Sensor.TYPE_PROXIMITY:
// Set the proximity sensor text view to the light sensor
// string from the resources, with the placeholder filled in.
mTextSensorProximity.setText(getResources().getString(
R.string.label_proximity, currentValue));
break;
default:
// do nothing
}
}
/**
* Abstract method in SensorEventListener. It must be implemented, but is
* unused in this app.
*/
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
class ClientThread implements Runnable {
private final String message;
ClientThread(String message) {
this.message = message;
}
@Override
public void run() {
try {
URL yahoo = new URL("http://roger123.pythonanywhere.com/dummy/");
HttpURLConnection con = (HttpURLConnection) yahoo.openConnection();
con.setDoOutput(true);
con.addRequestProperty("Content-Type", "application/"+"POST");
con.getOutputStream().write(message.getBytes());
System.out.println(con.getResponseCode());
con.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
// updating the UI
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
}
}