Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. private boolean mRun;
  2. private SimpleDateFormat format;
  3. private NotificationManager mNotificationManager;
  4. private Notification.Builder mBuilder;
  5. private Context mContext;
  6.  
  7. CheckboxThread(Context context){
  8. super();
  9. mContext=context;
  10. format = new SimpleDateFormat("hh:mm");
  11. mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  12. mBuilder = new Notification.Builder(context)
  13. .setContentTitle("Task reminder")
  14. .setSmallIcon(R.drawable.red);
  15. }
  16.  
  17. @Override
  18. public synchronized void start() {
  19. mRun = true;
  20. super.start();
  21. }
  22.  
  23. synchronized void exit() {
  24. mRun = false;
  25. }
  26.  
  27. @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
  28. @Override
  29. public void run() {
  30. while(mRun){
  31. String msg = "15 минута до истека задатка: ";
  32. boolean notiHasItems=false;
  33. for (Zadatak t:MainActivity.zadaci) {
  34. if (t.getmDate().equals(mContext.getResources().getString(R.string.danas)) && !t.getChecked() && t.getmAlarm()!=0 ) {
  35. Calendar current = Calendar.getInstance();
  36. Calendar taskTime = Calendar.getInstance();
  37. try {
  38. taskTime.setTime(format.parse(t.getmTime()));
  39. } catch (ParseException e) {
  40. e.printStackTrace();
  41. }
  42. if ( taskTime.get(Calendar.HOUR_OF_DAY) == current.get(Calendar.HOUR_OF_DAY) ) {
  43. if(taskTime.get(Calendar.MINUTE)-current.get(Calendar.MINUTE)<=15 && taskTime.get(Calendar.MINUTE)-current.get(Calendar.MINUTE)>=0) {
  44. if (notiHasItems)
  45. msg += " , " + t.getmText();
  46. else
  47. msg += t.getmText();
  48. notiHasItems = true;
  49. }
  50. }else if (taskTime.get(Calendar.HOUR_OF_DAY) - current.get(Calendar.HOUR_OF_DAY) == 1) {
  51. if(taskTime.get(Calendar.MINUTE)+60-current.get(Calendar.MINUTE)<=15 && taskTime.get(Calendar.MINUTE)+60-current.get(Calendar.MINUTE)>=0){
  52. if (notiHasItems)
  53. msg += " , " + t.getmText();
  54. else
  55. msg += t.getmText();
  56. notiHasItems = true;
  57. }
  58. }
  59. }
  60. }
  61. if(notiHasItems) {
  62. mBuilder.setContentTitle(msg);
  63. mNotificationManager.notify(0, mBuilder.build());
  64. }else{
  65. mNotificationManager.cancel(0);
  66. }
  67. try {
  68. long PERIOD = 2000;
  69. sleep(PERIOD);
  70. } catch (InterruptedException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement