Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.32 KB | None | 0 0
  1. package io.github.boapps.eSzivacs.Utils;
  2.  
  3. import android.app.Activity;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.graphics.Color;
  10. import android.net.ConnectivityManager;
  11. import android.net.NetworkInfo;
  12. import android.os.Bundle;
  13. import android.os.Handler;
  14. import android.support.v4.app.NotificationCompat;
  15. import android.util.Log;
  16. import android.widget.Toast;
  17.  
  18. import com.google.android.gms.gcm.GcmNetworkManager;
  19. import com.google.android.gms.gcm.GcmTaskService;
  20. import com.google.android.gms.gcm.OneoffTask;
  21. import com.google.android.gms.gcm.PeriodicTask;
  22. import com.google.android.gms.gcm.Task;
  23. import com.google.android.gms.gcm.TaskParams;
  24.  
  25. import java.io.IOException;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28.  
  29. import io.github.boapps.eSzivacs.Activities.EvaluationListActivity;
  30. import io.github.boapps.eSzivacs.Datas.Evaluation;
  31. import io.github.boapps.eSzivacs.R;
  32.  
  33. public class BackgroundTaskService extends GcmTaskService {
  34.  
  35.     public static final String GCM_ONEOFF_TAG = "oneoff|[0,0]";
  36.     public static final String GCM_REPEAT_TAG = "repeat|[7200,1800]";
  37.     private static final String TAG = BackgroundTaskService.class.getSimpleName();
  38.  
  39.  
  40.     @Override
  41.     public void onInitializeTasks() {
  42.         //called when app is updated to a new version, reinstalled etc.
  43.         //you have to schedule your repeating tasks again
  44.         super.onInitializeTasks();
  45.     }
  46.  
  47.     @Override
  48.     public int onRunTask(TaskParams taskParams) {
  49.         Bundle extras = taskParams.getExtras();
  50.  
  51.         Handler h = new Handler(getMainLooper());
  52.         Log.v(TAG, "onRunTask");
  53.         if (taskParams.getTag().equals(GCM_ONEOFF_TAG)) {
  54.             h.post(new Runnable() {
  55.                 @Override
  56.                 public void run() {
  57.                     Toast.makeText(BackgroundTaskService.this, "ONEOFF executed", Toast.LENGTH_SHORT).show();
  58.                 }
  59.             });
  60.         } else if (taskParams.getTag().equals(GCM_REPEAT_TAG)) {
  61.             h.post(new Runnable() {
  62.                 @Override
  63.                 public void run() {
  64.                     if (isOnline()) {
  65.                         AccountManager accountManager = new AccountManager(getApplicationContext());
  66.                         String usr = accountManager.getSelectedAccount();
  67.                         SharedPreferences sharedPreferences = getApplication().getSharedPreferences(usr, Activity.MODE_PRIVATE);
  68.                         String psw = sharedPreferences.getString("pw", "");
  69.                         String schoolCode = sharedPreferences.getString("schoolCode", "");
  70.                         String schoolUrl = sharedPreferences.getString("schoolUrl", "");
  71.  
  72.                         DataLoader dloader = new DataLoader(getApplicationContext());
  73.                         dloader.setLogin(usr, psw, schoolUrl, schoolCode);
  74.                         try {
  75.                             dloader.doLogin();
  76.                         } catch (IOException e) {
  77.                             e.printStackTrace();
  78.                         }
  79.  
  80.                         ArrayList<Evaluation> evaluations = dloader.getNewEvaluations();
  81.  
  82.                         for (Evaluation evaluation : evaluations) {
  83.                             System.out.println(evaluation.getSubject() + evaluation.getNumericValue());
  84.                             String CHANNEL_ID = "jegyek";
  85.                             NotificationCompat.Builder mBuilder =
  86.                                     new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
  87.                                             .setSmallIcon(R.drawable.ic_school_black_24px)
  88.                                             .setContentTitle(evaluation.getSubject() + " " + evaluation.getNumericValue())
  89.                                             .setColor(Color.parseColor("#bf360c"))
  90.                                             .setContentText(evaluation.getTheme());
  91.                             NotificationManager mNotificationManager =
  92.                                     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  93.                             int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
  94.                             Intent intent = new Intent(getApplicationContext(), EvaluationListActivity.class);
  95.                             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
  96.                                     | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  97.  
  98.                             PendingIntent pendingIntentintent = PendingIntent.getActivity(getApplicationContext(), 0,
  99.                                     intent, 0);
  100.                             mBuilder.setContentIntent(pendingIntentintent);
  101.  
  102.                             mNotificationManager.notify(m, mBuilder.build());
  103.  
  104.                         }
  105.                     }
  106.  
  107.  
  108.                 }
  109.             });
  110.         }
  111.         return GcmNetworkManager.RESULT_SUCCESS;
  112.     }
  113.  
  114.     public boolean isOnline() {
  115.         ConnectivityManager cm =
  116.                 (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
  117.         NetworkInfo netInfo = cm.getActiveNetworkInfo();
  118.         return netInfo != null && netInfo.isConnectedOrConnecting();
  119.     }
  120.  
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement