Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. package com.gikdew.emaildemo;
  2.  
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7.  
  8. import org.apache.http.NameValuePair;
  9. import org.json.JSONArray;
  10. import org.json.JSONException;
  11. import org.json.JSONObject;
  12.  
  13. import android.app.Notification;
  14. import android.app.NotificationManager;
  15. import android.app.PendingIntent;
  16. import android.app.Service;
  17. import android.content.Context;
  18. import android.content.Intent;
  19. import android.content.SharedPreferences;
  20. import android.content.SharedPreferences.Editor;
  21. import android.os.AsyncTask;
  22. import android.os.Handler;
  23. import android.os.IBinder;
  24. import android.support.v4.app.NotificationCompat;
  25. import android.util.Log;
  26. import android.widget.ListView;
  27.  
  28. public class DownloadService extends Service {
  29.  
  30.        
  31.     // Creating JSON Parser object
  32.     JSONParser jParser = new JSONParser();
  33.  
  34.     ArrayList<HashMap<String, String>> productsList;
  35.  
  36.     // url to get all products list
  37.     private static String url_all_products = "http://gikdew.com/android/emails/get_last_signal.php";
  38.  
  39.     // JSON Node names
  40.     private static final String TAG_SUCCESS = "success";
  41.     private static final String TAG_PRODUCTS = "signals";
  42.     private static final String TAG_ID = "id";
  43.     private static final String TAG_pair = "pair";
  44.     private static final String TAG_expiry = "expiry";
  45.     private static final String TAG_result = "result";
  46.     private static final String TAG_created_at = "created_at";
  47.     String pair, expiry, result;
  48.  
  49.     ListView list;
  50.  
  51.     // products JSONArray
  52.     JSONArray products = null;
  53.    
  54.     @
  55.     Override
  56.     public int onStartCommand(Intent intent, int flags, int startId) {
  57.         //Log.i("Service", "Started");
  58.         new DownloaderTask().execute();
  59.         return START_STICKY;
  60.     }
  61.  
  62.     @
  63.     Override
  64.     public IBinder onBind(Intent intent) {
  65.         return null;
  66.     }
  67.    
  68.     private class DownloaderTask extends AsyncTask < URL, Void, String > {
  69.         final SharedPreferences signalsPref = getSharedPreferences("signals", Context.MODE_PRIVATE);
  70.         final Editor editor = signalsPref.edit();
  71.  
  72.         @
  73.         Override
  74.         protected void onPreExecute() {
  75.         }
  76.  
  77.         @
  78.         Override
  79.         protected String doInBackground(URL...url) {
  80.             List<NameValuePair> params = new ArrayList<NameValuePair>();
  81.             // getting JSON string from URL
  82.             JSONObject json = jParser.makeHttpRequest(url_all_products, "GET",
  83.                     params);
  84.  
  85.             // Check your log cat for JSON reponse
  86.             Log.d("All Products: ", json.toString());
  87.  
  88.             try {
  89.                 // Checking for SUCCESS TAG
  90.                 int success = json.getInt(TAG_SUCCESS);
  91.  
  92.                 if (success == 1) {
  93.                     // products found
  94.                     // Getting Array of Products
  95.                     products = json.getJSONArray(TAG_PRODUCTS);
  96.  
  97.                     // looping through All Products
  98.                     for (int i = 0; i < products.length(); i++) {
  99.                         JSONObject c = products.getJSONObject(i);
  100.  
  101.                         // Storing each json item in variable
  102.                         String id = c.getString(TAG_ID);
  103.  
  104.                         pair = c.getString(TAG_pair);
  105.                         expiry = c.getString(TAG_expiry);
  106.                         result = c.getString(TAG_result);
  107.                         //createat = c.getString(TAG_created_at);
  108.  
  109.                         if (i == 0) {
  110.                             SharedPreferences signalsPref = getSharedPreferences(
  111.                                     "signals", 0);
  112.                             Editor editor = signalsPref.edit();
  113.  
  114.                             editor.putString("lastSignal1", id);
  115.                             editor.commit();
  116.  
  117.                             Log.d("lastSignal",
  118.                                     signalsPref.getString("lastSignal1", "0"));
  119.                         }
  120.                     }
  121.                 } else {
  122.                     Log.d("Error", "DownloadServiceError");
  123.                 }
  124.             } catch (JSONException e) {
  125.                 e.printStackTrace();
  126.             }
  127.  
  128.             return null;
  129.         }
  130.  
  131.         @
  132.         Override
  133.         protected void onPostExecute(String result) {
  134.             if(!signalsPref.getString("lastSignal1", "0").equals(signalsPref.getString("lastSignal", "0"))) {
  135.                
  136.                 editor.putString("lastSignal", signalsPref.getString("lastSignal1", "0"));
  137.                 editor.commit();
  138.                
  139.                 sendNotification();
  140.  
  141.             } else {
  142.                
  143.             }
  144.             new Handler().postDelayed(new Runnable() {
  145.                 @Override
  146.                 public void run() {
  147.                     new DownloaderTask().execute();
  148.                 }
  149.             }, 7000);
  150.         }
  151.  
  152.         public void sendNotification() {
  153.             Intent intent = new Intent(DownloadService.this, App.class);
  154.             intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
  155.             PendingIntent pIntent = PendingIntent.getActivity(DownloadService.this, 0, intent, 0);
  156.  
  157.             // build notification
  158.             // the addAction re-use the same intent to keep the example short
  159.             NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(DownloadService.this);
  160.  
  161.             mBuilder.setContentTitle(pair)
  162.                 .setContentText(expiry + " " + result)
  163.                 .setTicker(pair + " " + expiry + " " + result)
  164.                 .setSmallIcon(R.drawable.ic_launcher)
  165.                 .setContentIntent(pIntent)
  166.                 .setAutoCancel(true)
  167.                 .setDefaults(Notification.DEFAULT_ALL);
  168.  
  169.             NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  170.  
  171.             notificationManager.notify(0, mBuilder.build());
  172.  
  173.         }
  174.     }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement