Advertisement
Guest User

Service

a guest
May 27th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. package com.example.root.myapplication1;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.app.Service;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.IBinder;
  10. import android.support.v4.app.NotificationCompat;
  11.  
  12. import java.util.Calendar;
  13. import java.util.Date;
  14. import java.util.concurrent.TimeUnit;
  15.  
  16. public class Alarms extends Service {
  17.     DBPlanner dbPlanner;
  18.     Context context;
  19.     Calendar calendar;
  20.     NotificationManager notificationManager;
  21.     @Override
  22.     public int onStartCommand(Intent intent, int flags, int startId) {
  23.  
  24.         task();
  25.         return super.onStartCommand(intent, flags, startId);
  26.     }
  27.  
  28.     @Override
  29.     public void onCreate() {
  30.         dbPlanner=new DBPlanner(getApplicationContext());
  31.         context=this;
  32.  
  33.         super.onCreate();
  34.     }
  35.  
  36.     @Override
  37.     public void onDestroy() {
  38.         super.onDestroy();
  39.     }
  40.  
  41.     @Override
  42.     public IBinder onBind(Intent intent) {
  43.         return null;
  44.     }
  45.     void task(){
  46.  
  47.         new Thread(new Runnable() {
  48.             @Override
  49.             public void run() {
  50.  
  51.                 while (true){
  52.                     calendar=Calendar.getInstance();
  53.                     Integer hours=calendar.get(Calendar.HOUR_OF_DAY);
  54.                     Integer minutes=calendar.get(Calendar.MINUTE);
  55.                     String month,day;
  56.                     if(calendar.get(Calendar.MONTH)<=9){
  57.                         month="0"+calendar.get(Calendar.MONTH);
  58.                     }else{
  59.                         month=calendar.get(Calendar.MONTH)+"";
  60.                     }
  61.                     if(calendar.get(Calendar.DAY_OF_MONTH)<=9){
  62.                         day="0"+calendar.get(Calendar.DAY_OF_MONTH);
  63.                     }else{
  64.                         day=calendar.get(Calendar.DAY_OF_MONTH)+"";
  65.                     }
  66.                     Plans plan=dbPlanner.find(hours,minutes,day+"/"+month+"/"+calendar.get(Calendar.YEAR));
  67.                     if(plan.getPlan()!=null){
  68.                         NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
  69.                                         .setSmallIcon(R.mipmap.ic_launcher)
  70.                                         .setContentTitle("Напоминание")
  71.                                         .setContentText(plan.getPlan());
  72.                         Notification notification = builder.build();
  73.                         notification.flags |=Notification.FLAG_AUTO_CANCEL;
  74.                         NotificationManager notificationManager =
  75.                                 (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  76.  
  77.                         notificationManager.notify(1, notification);
  78.  
  79.  
  80.                     }
  81.                     try {
  82.                         TimeUnit.SECONDS.sleep(60);
  83.                     } catch (InterruptedException e) {
  84.                         e.printStackTrace();
  85.                     }
  86.                 }
  87.             }
  88.         }).start();
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement