Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. long nextExecute = getNextExecute(t);
  2.  
  3. if (nextExecute > System.currentTimeMillis()) {
  4.  
  5. int tt = 12;
  6.  
  7. intent = new Intent(context, BirthExecutor.class);
  8. intent.putExtra(Target.ROW_ID, tt);
  9.  
  10. PendingIntent pending = PendingIntent.getBroadcast(context, 10, intent, PendingIntent.FLAG_NO_CREATE);
  11.  
  12. if (pending != null) {
  13.  
  14. manager.set(AlarmManager.RTC_WAKEUP, nextExecute, pending);
  15.  
  16. BirthLog log = new BirthLog("Scheduled " + t.getFirstname() + " " + t.getLastname() + " on " + df.format(nextExecute));
  17. log_helper.insertLog(log);
  18.  
  19. } else {
  20.  
  21. BirthLog log = new BirthLog(t.getFirstname() + " " + t.getLastname() + " already active!");
  22. log_helper.insertLog(log);
  23.  
  24. }
  25.  
  26. intent = new Intent(LogAdapter.LOG_RECEIVE_ACTION);
  27. context.sendBroadcast(intent);
  28.  
  29. }
  30.  
  31. public void updateTask(Target t) {
  32.  
  33. if (t.getTime() == null || t.getRow() == null)
  34. return;
  35.  
  36. Intent intent;
  37. SimpleDateFormat df = new SimpleDateFormat("HH:mm dd.MM.yyyy", Locale.US);
  38.  
  39. long nextExecute = getNextExecute(t);
  40.  
  41. if (nextExecute > System.currentTimeMillis()) {
  42.  
  43. intent = new Intent(static_context, BirthExecutor.class);
  44. intent.putExtra(Target.ROW_ID, 12);
  45.  
  46. PendingIntent pending = PendingIntent.getBroadcast(static_context, 10, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  47.  
  48. if (pending != null) {
  49.  
  50. manager.set(AlarmManager.RTC_WAKEUP, nextExecute, pending);
  51.  
  52. BirthLog log = new BirthLog("Scheduled " + t.getFirstname() + " " + t.getLastname() + " on " + df.format(nextExecute));
  53. log_helper.insertLog(log);
  54. Log.d("birth", log.getComment());
  55.  
  56. } else {
  57.  
  58. BirthLog log = new BirthLog(t.getFirstname() + " " + t.getLastname() + " ERROR. TIME: " + df.format(nextExecute));
  59. log_helper.insertLog(log);
  60. Log.d("birth", log.getComment());
  61.  
  62. }
  63.  
  64. intent = new Intent(LogAdapter.LOG_RECEIVE_ACTION);
  65. static_context.sendBroadcast(intent);
  66.  
  67. }
  68.  
  69. }
  70.  
  71. @Override
  72. protected Void doInBackground(Void... params) {
  73.  
  74. Intent intent;
  75. SimpleDateFormat df = new SimpleDateFormat("HH:mm dd.MM.yyyy", Locale.US);
  76.  
  77. for (Target t : target_helper.getAllTarget()) {
  78.  
  79. long nextExecute = getNextExecute(t);
  80.  
  81. if (nextExecute > System.currentTimeMillis()) {
  82.  
  83. PendingIntent pending = getPendingIntent(context, t, false);
  84.  
  85. if (pending != null) {
  86.  
  87. BirthLog log = new BirthLog(t.getFirstname() + " " + t.getLastname() + " already active!");
  88. log_helper.insertLog(log);
  89.  
  90. } else {
  91.  
  92. manager.set(AlarmManager.RTC_WAKEUP, nextExecute, pending);
  93.  
  94. BirthLog log = new BirthLog("Scheduled " + t.getFirstname() + " " + t.getLastname() + " on " + df.format(nextExecute));
  95. log_helper.insertLog(log);
  96.  
  97. }
  98.  
  99. intent = new Intent(LogAdapter.LOG_RECEIVE_ACTION);
  100. context.sendBroadcast(intent);
  101.  
  102. }
  103.  
  104. }
  105.  
  106. return null;
  107.  
  108. }
  109.  
  110. private PendingIntent getPendingIntent(Context context, Target t, boolean update) {
  111.  
  112. Intent intent = new Intent(context, BirthExecutor.class);
  113. intent.putExtra(Target.ROW_ID, t.getRow());
  114.  
  115. if (update) {
  116. return PendingIntent.getBroadcast(context, t.getRow().intValue(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
  117. } else {
  118. return PendingIntent.getBroadcast(context, t.getRow().intValue(), intent, PendingIntent.FLAG_NO_CREATE);
  119. }
  120.  
  121. }
  122.  
  123. AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  124. Intent intent = new Intent(context, AlarmReceiver.class);
  125. PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
  126. if (pendingIntent == null) {
  127. pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  128. // start it only it wasn't running already
  129. alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 60 * 1000, pendingIntent);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement