Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.nihal.myapplication;
- import android.app.Service;
- import android.content.Context;
- import android.os.AsyncTask;
- import android.os.Handler;
- import android.os.IBinder;
- import android.content.Intent;
- import android.telephony.SmsManager;
- import android.util.Log;
- import android.widget.Toast;
- import com.kosalgeek.asynctask.PostResponseAsyncTask;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import static android.content.ContentValues.TAG;
- /**
- * Created by Boiijek on 21/10/2017.
- */
- public class TimeService extends Service {
- public Context context = this;
- public Handler handler = null;
- public static Runnable runnable = null;
- private static String TAG = TimeService.class.getSimpleName();
- private MyThread mythread;
- public boolean isRunning = false;
- String data = "";
- String dataParsed = "";
- String singleParsed = "";
- @Override
- public void onCreate() {
- super.onCreate();
- Log.d(TAG, "onCreate");
- mythread = new MyThread();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Toast.makeText(this, "Monitoring has begun!", Toast.LENGTH_LONG).show();
- // Thread thread = new Thread(new MyThreadClass(startId));
- // thread.start();
- handler = new Handler();
- runnable = new Runnable() {
- public void run() {
- Toast.makeText(context, "Service is still running", Toast.LENGTH_LONG).show();
- handler.postDelayed(runnable, 10000);
- Log.d(TAG, "onStart");
- if(!isRunning){
- mythread.start();
- isRunning = true;
- }
- }
- };
- handler.postDelayed(runnable, 15000);
- return START_STICKY;
- }
- @Override
- public void onDestroy() {
- handler.removeCallbacks(runnable);
- Toast.makeText(this, "Monitoring has stopped!", Toast.LENGTH_LONG).show();
- if(!isRunning){
- mythread.interrupt();
- mythread.stop();
- }
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- public void readWebPage() {
- try {
- URL url = new URL("https://api.myjson.com/bins/1bkn5b");
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- String line = "";
- while (line != null) {
- line = bufferedReader.readLine();
- data = data + line;
- }
- JSONArray JA = new JSONArray(data);
- //Print the entire line of json
- // for(int i =0 ; i<JA.length(); i++){
- //Print the last 5 values of the array
- for (int i = JA.length() - 1; i > 6; i--) {
- JSONObject JO = (JSONObject) JA.get(i);
- singleParsed = "Datetime:" + JO.get("datetime") + "\n" +
- "Temperature:" + JO.get("tempData") + "\n";
- int temp = JO.getInt("tempData");
- if (temp > 40) {
- try {
- String phoneNo = "91827646";
- String msg = "Machine is OVERHEATING. Please attend to it ASAP!";
- SmsManager smsManager = SmsManager.getDefault();
- smsManager.sendTextMessage(phoneNo, null, msg, null, null);
- Log.d("SMSSSSS", "SMS sent");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- dataParsed = dataParsed + singleParsed + "\n" + "\n" + "\n" + "\n";
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- class MyThread extends Thread {
- static final long DELAY = 3000;
- @Override
- public void run() {
- while (isRunning) {
- Log.d(TAG, "Running");
- try {
- readWebPage();
- Thread.sleep(DELAY);
- } catch (InterruptedException e) {
- isRunning = false;
- e.printStackTrace();
- }
- }
- class DoBackgroundTask extends AsyncTask<Void, Void, Void> {
- String data = "";
- String dataParsed = "";
- String singleParsed = "";
- @Override
- public Void doInBackground(Void... voids) {
- try {
- URL url = new URL("https://api.myjson.com/bins/1bkn5b");
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- String line = "";
- while (line != null) {
- line = bufferedReader.readLine();
- data = data + line;
- }
- JSONArray JA = new JSONArray(data);
- //Print the entire line of json
- // for(int i =0 ; i<JA.length(); i++){
- //Print the last 5 values of the array
- for (int i = JA.length() - 1; i > 6; i--) {
- JSONObject JO = (JSONObject) JA.get(i);
- singleParsed = "Datetime:" + JO.get("datetime") + "\n" +
- "Temperature:" + JO.get("tempData") + "\n";
- int temp = JO.getInt("tempData");
- if (temp > 40) {
- try {
- String phoneNo = "91827646";
- String msg = "Machine is OVERHEATING. Please attend to it ASAP!";
- SmsManager smsManager = SmsManager.getDefault();
- smsManager.sendTextMessage(phoneNo, null, msg, null, null);
- Log.d("SMSSSSS", "SMS sent");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- dataParsed = dataParsed + singleParsed + "\n" + "\n" + "\n" + "\n";
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- public void onPostExecute(Void aVoid) {
- super.onPostExecute(aVoid);
- LocalService.data.setText(dataParsed);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement