package stroum videotemp import android content Intent import android

  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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package stroum.videotemp;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.util.logging.Handler;
public class MainActivity extends ActionBarActivity {
private TextView textView;
private TextView speedVal;
private String addr = "192.168.1.64";
private String port = "3333";
private int interval = 5000;
private String TAG = "TEMPERATURE";
private SharedPreferences sp;
private Thread myThread = null;
private Socket s;
volatile boolean activityStopped = false;
private class GetTempTask extends AsyncTask<URL, Integer, String> {
@Override
protected String doInBackground(URL... params) {
try {
s = new Socket(addr, Integer.parseInt(port));
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String st = input.readLine();
s.close();
return st;
/*URL url = new URL(addr);
BufferedReader in = new BufferedReader(
new InputStreamReader(
url.openStream()));
String inputLine;
String result = "";
while ((inputLine = in.readLine()) != null) {
result += inputLine;
}
in.close();
return result;*/
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String res) {
try {
JSONObject j = new JSONObject(res);
String tmp = j.getString("tmp");
String speed = j.getString("speed");
Log.d(TAG, tmp);
Log.d(TAG, speed);
Log.d(TAG, "" + interval);
textView.setText(tmp + "° C");
speedVal.setText(speed + "%");
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "JSON Parse error, restart app", Toast.LENGTH_LONG).show();
// Log.e(TAG, "wtf?");
activityStopped = true;
myThread.interrupt();
}
}
}
private void getTemp() {
new GetTempTask().execute();
Runnable updateTask = new Runnable() {
@Override
public void run() {
while (!activityStopped) {
try {
Thread.sleep(interval);
new GetTempTask().execute();
} catch (Exception e) {
}
}
}
};
myThread = new Thread(updateTask);
myThread.start();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.temp);
speedVal = (TextView) findViewById(R.id.speedVal);
sp = PreferenceManager.getDefaultSharedPreferences(this);
getTemp();
}
public void onPause() {
super.onPause();
activityStopped = true;
// myThread.interrupt();
}
public void onResume() {
super.onResume();
Log.d(TAG, "" + myThread.isAlive());
activityStopped = false;
interval = Integer.parseInt(sp.getString("interval", ""));
addr = sp.getString("address", "");
port = sp.getString("port", "3333");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
}