Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.root.myapplication1;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.os.IBinder;
- import android.support.v4.app.NotificationCompat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.concurrent.TimeUnit;
- public class Alarms extends Service {
- DBPlanner dbPlanner;
- Context context;
- Calendar calendar;
- NotificationManager notificationManager;
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- task();
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public void onCreate() {
- dbPlanner=new DBPlanner(getApplicationContext());
- context=this;
- super.onCreate();
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- void task(){
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (true){
- calendar=Calendar.getInstance();
- Integer hours=calendar.get(Calendar.HOUR_OF_DAY);
- Integer minutes=calendar.get(Calendar.MINUTE);
- String month,day;
- if(calendar.get(Calendar.MONTH)<=9){
- month="0"+calendar.get(Calendar.MONTH);
- }else{
- month=calendar.get(Calendar.MONTH)+"";
- }
- if(calendar.get(Calendar.DAY_OF_MONTH)<=9){
- day="0"+calendar.get(Calendar.DAY_OF_MONTH);
- }else{
- day=calendar.get(Calendar.DAY_OF_MONTH)+"";
- }
- Plans plan=dbPlanner.find(hours,minutes,day+"/"+month+"/"+calendar.get(Calendar.YEAR));
- if(plan.getPlan()!=null){
- NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
- .setSmallIcon(R.mipmap.ic_launcher)
- .setContentTitle("Напоминание")
- .setContentText(plan.getPlan());
- Notification notification = builder.build();
- notification.flags |=Notification.FLAG_AUTO_CANCEL;
- NotificationManager notificationManager =
- (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- notificationManager.notify(1, notification);
- }
- try {
- TimeUnit.SECONDS.sleep(60);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement