Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. package com.example.nihal.myapplication;
  2.  
  3. import android.app.Service;
  4. import android.content.Context;
  5. import android.os.AsyncTask;
  6. import android.os.Handler;
  7. import android.os.IBinder;
  8. import android.content.Intent;
  9. import android.telephony.SmsManager;
  10. import android.util.Log;
  11. import android.widget.Toast;
  12.  
  13. import com.kosalgeek.asynctask.PostResponseAsyncTask;
  14.  
  15. import org.apache.http.HttpEntity;
  16. import org.apache.http.HttpResponse;
  17. import org.apache.http.client.ClientProtocolException;
  18. import org.apache.http.client.HttpClient;
  19. import org.apache.http.client.methods.HttpPost;
  20. import org.apache.http.entity.StringEntity;
  21. import org.apache.http.impl.client.DefaultHttpClient;
  22. import org.apache.http.util.EntityUtils;
  23. import org.json.JSONArray;
  24. import org.json.JSONException;
  25. import org.json.JSONObject;
  26.  
  27. import java.io.BufferedReader;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.io.InputStreamReader;
  31. import java.net.HttpURLConnection;
  32. import java.net.MalformedURLException;
  33. import java.net.URL;
  34.  
  35. import static android.content.ContentValues.TAG;
  36.  
  37.  
  38. /**
  39. * Created by Boiijek on 21/10/2017.
  40. */
  41.  
  42. public class TimeService extends Service {
  43. public Context context = this;
  44. public Handler handler = null;
  45. public static Runnable runnable = null;
  46. private static String TAG = TimeService.class.getSimpleName();
  47. private MyThread mythread;
  48. public boolean isRunning = false;
  49.  
  50. String data = "";
  51. String dataParsed = "";
  52. String singleParsed = "";
  53.  
  54.  
  55. @Override
  56. public void onCreate() {
  57. super.onCreate();
  58. Log.d(TAG, "onCreate");
  59. mythread = new MyThread();
  60. }
  61.  
  62.  
  63. @Override
  64. public int onStartCommand(Intent intent, int flags, int startId) {
  65.  
  66. Toast.makeText(this, "Monitoring has begun!", Toast.LENGTH_LONG).show();
  67. // Thread thread = new Thread(new MyThreadClass(startId));
  68. // thread.start();
  69. handler = new Handler();
  70. runnable = new Runnable() {
  71. public void run() {
  72.  
  73. Toast.makeText(context, "Service is still running", Toast.LENGTH_LONG).show();
  74. handler.postDelayed(runnable, 10000);
  75. Log.d(TAG, "onStart");
  76. if(!isRunning){
  77. mythread.start();
  78. isRunning = true;
  79. }
  80.  
  81. }
  82. };
  83.  
  84. handler.postDelayed(runnable, 15000);
  85. return START_STICKY;
  86. }
  87.  
  88.  
  89. @Override
  90. public void onDestroy() {
  91. handler.removeCallbacks(runnable);
  92. Toast.makeText(this, "Monitoring has stopped!", Toast.LENGTH_LONG).show();
  93. if(!isRunning){
  94. mythread.interrupt();
  95. mythread.stop();
  96. }
  97. }
  98.  
  99.  
  100. @Override
  101. public IBinder onBind(Intent intent) {
  102. return null;
  103. }
  104.  
  105. public void readWebPage() {
  106.  
  107. try {
  108. URL url = new URL("https://api.myjson.com/bins/1bkn5b");
  109. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  110. InputStream inputStream = httpURLConnection.getInputStream();
  111. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  112. String line = "";
  113. while (line != null) {
  114. line = bufferedReader.readLine();
  115. data = data + line;
  116. }
  117.  
  118. JSONArray JA = new JSONArray(data);
  119. //Print the entire line of json
  120. // for(int i =0 ; i<JA.length(); i++){
  121.  
  122. //Print the last 5 values of the array
  123. for (int i = JA.length() - 1; i > 6; i--) {
  124.  
  125. JSONObject JO = (JSONObject) JA.get(i);
  126.  
  127. singleParsed = "Datetime:" + JO.get("datetime") + "\n" +
  128. "Temperature:" + JO.get("tempData") + "\n";
  129.  
  130. int temp = JO.getInt("tempData");
  131. if (temp > 40) {
  132. try {
  133.  
  134. String phoneNo = "91827646";
  135. String msg = "Machine is OVERHEATING. Please attend to it ASAP!";
  136. SmsManager smsManager = SmsManager.getDefault();
  137. smsManager.sendTextMessage(phoneNo, null, msg, null, null);
  138. Log.d("SMSSSSS", "SMS sent");
  139. } catch (Exception ex) {
  140. ex.printStackTrace();
  141. }
  142. }
  143.  
  144. dataParsed = dataParsed + singleParsed + "\n" + "\n" + "\n" + "\n";
  145.  
  146. }
  147.  
  148. } catch (MalformedURLException e) {
  149. e.printStackTrace();
  150. } catch (IOException e) {
  151. e.printStackTrace();
  152. } catch (JSONException e) {
  153. e.printStackTrace();
  154. }
  155.  
  156. }
  157.  
  158.  
  159.  
  160. class MyThread extends Thread {
  161. static final long DELAY = 3000;
  162.  
  163. @Override
  164. public void run() {
  165. while (isRunning) {
  166. Log.d(TAG, "Running");
  167. try {
  168. readWebPage();
  169. Thread.sleep(DELAY);
  170. } catch (InterruptedException e) {
  171. isRunning = false;
  172. e.printStackTrace();
  173. }
  174. }
  175.  
  176. class DoBackgroundTask extends AsyncTask<Void, Void, Void> {
  177.  
  178.  
  179. String data = "";
  180. String dataParsed = "";
  181. String singleParsed = "";
  182.  
  183.  
  184. @Override
  185. public Void doInBackground(Void... voids) {
  186. try {
  187. URL url = new URL("https://api.myjson.com/bins/1bkn5b");
  188. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  189. InputStream inputStream = httpURLConnection.getInputStream();
  190. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  191. String line = "";
  192. while (line != null) {
  193. line = bufferedReader.readLine();
  194. data = data + line;
  195. }
  196.  
  197. JSONArray JA = new JSONArray(data);
  198. //Print the entire line of json
  199. // for(int i =0 ; i<JA.length(); i++){
  200.  
  201. //Print the last 5 values of the array
  202. for (int i = JA.length() - 1; i > 6; i--) {
  203.  
  204. JSONObject JO = (JSONObject) JA.get(i);
  205.  
  206. singleParsed = "Datetime:" + JO.get("datetime") + "\n" +
  207. "Temperature:" + JO.get("tempData") + "\n";
  208.  
  209. int temp = JO.getInt("tempData");
  210. if (temp > 40) {
  211. try {
  212.  
  213. String phoneNo = "91827646";
  214. String msg = "Machine is OVERHEATING. Please attend to it ASAP!";
  215. SmsManager smsManager = SmsManager.getDefault();
  216. smsManager.sendTextMessage(phoneNo, null, msg, null, null);
  217. Log.d("SMSSSSS", "SMS sent");
  218. } catch (Exception ex) {
  219. ex.printStackTrace();
  220. }
  221. }
  222.  
  223. dataParsed = dataParsed + singleParsed + "\n" + "\n" + "\n" + "\n";
  224.  
  225. }
  226.  
  227. } catch (MalformedURLException e) {
  228. e.printStackTrace();
  229. } catch (IOException e) {
  230. e.printStackTrace();
  231. } catch (JSONException e) {
  232. e.printStackTrace();
  233. }
  234.  
  235. return null;
  236. }
  237.  
  238. @Override
  239. public void onPostExecute(Void aVoid) {
  240.  
  241. super.onPostExecute(aVoid);
  242. LocalService.data.setText(dataParsed);
  243.  
  244. }
  245.  
  246. }
  247.  
  248. }
  249. }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement