Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private boolean mRun;
- private SimpleDateFormat format;
- private NotificationManager mNotificationManager;
- private Notification.Builder mBuilder;
- private Context mContext;
- CheckboxThread(Context context){
- super();
- mContext=context;
- format = new SimpleDateFormat("hh:mm");
- mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- mBuilder = new Notification.Builder(context)
- .setContentTitle("Task reminder")
- .setSmallIcon(R.drawable.red);
- }
- @Override
- public synchronized void start() {
- mRun = true;
- super.start();
- }
- synchronized void exit() {
- mRun = false;
- }
- @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
- @Override
- public void run() {
- while(mRun){
- String msg = "15 минута до истека задатка: ";
- boolean notiHasItems=false;
- for (Zadatak t:MainActivity.zadaci) {
- if (t.getmDate().equals(mContext.getResources().getString(R.string.danas)) && !t.getChecked() && t.getmAlarm()!=0 ) {
- Calendar current = Calendar.getInstance();
- Calendar taskTime = Calendar.getInstance();
- try {
- taskTime.setTime(format.parse(t.getmTime()));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- if ( taskTime.get(Calendar.HOUR_OF_DAY) == current.get(Calendar.HOUR_OF_DAY) ) {
- if(taskTime.get(Calendar.MINUTE)-current.get(Calendar.MINUTE)<=15 && taskTime.get(Calendar.MINUTE)-current.get(Calendar.MINUTE)>=0) {
- if (notiHasItems)
- msg += " , " + t.getmText();
- else
- msg += t.getmText();
- notiHasItems = true;
- }
- }else if (taskTime.get(Calendar.HOUR_OF_DAY) - current.get(Calendar.HOUR_OF_DAY) == 1) {
- if(taskTime.get(Calendar.MINUTE)+60-current.get(Calendar.MINUTE)<=15 && taskTime.get(Calendar.MINUTE)+60-current.get(Calendar.MINUTE)>=0){
- if (notiHasItems)
- msg += " , " + t.getmText();
- else
- msg += t.getmText();
- notiHasItems = true;
- }
- }
- }
- }
- if(notiHasItems) {
- mBuilder.setContentTitle(msg);
- mNotificationManager.notify(0, mBuilder.build());
- }else{
- mNotificationManager.cancel(0);
- }
- try {
- long PERIOD = 2000;
- sleep(PERIOD);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement