Advertisement
Guest User

Untitled

a guest
Sep 12th, 2014
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. public class MyService extends Service {
  2.  
  3.     private DBHelper dbHelper;
  4.     private SQLiteDatabase db;
  5.  
  6.     private int notificationID = 100;
  7.  
  8.     @Override
  9.     public void onCreate() {
  10.         Log.i("myLOgs", "Service: onCreate()");
  11.         super.onCreate();
  12.         dbHelper = new DBHelper(this);
  13.         db = dbHelper.getWritableDatabase();
  14.     }
  15.  
  16.     @Override
  17.     public int onStartCommand(Intent intent, int flags, int startId) {
  18.  
  19.         Log.i("myLOgs", "Service: onStartCommand()");
  20.  
  21.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  22.         Date date = new Date();
  23.         String currentDateString = dateFormat.format(date);
  24.         Log.i("myLOgs", currentDateString);
  25.  
  26.         SimpleDateFormat timeFormat = new SimpleDateFormat("HH-mm");
  27.         Date time = new Date();
  28.         String currentTimeString = timeFormat.format(time);
  29.         Log.i("myLogs", currentTimeString);
  30.  
  31.         String[] columns = {DBHelper.DATE, DBHelper.TIME, DBHelper.EVENT};
  32.         Cursor cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
  33.         cursor.moveToFirst(); // move cursor to the first raw
  34.  
  35.         do{
  36.             //get all info from database
  37.             String dateString = cursor.getString(cursor.getColumnIndex(DBHelper.DATE));
  38.             String timeString  = cursor.getString(cursor.getColumnIndex(DBHelper.TIME));
  39.             String eventString = cursor.getString(cursor.getColumnIndex(DBHelper.EVENT));
  40. //            Log.i("myLogs", dateString);
  41. //            Log.i("myLOgs", timeString);
  42. //            Log.i("myLogs", eventString);
  43.             boolean dateCompare = currentDateString.equals(dateString);
  44.             boolean timeCompare = currentTimeString.equals(timeString);
  45.             if((dateCompare) && (timeCompare)) {
  46.                 Notify(eventString, eventString); // send notification if current time is the same as the time from db
  47.                 break; //temporary
  48.             }
  49.             if(cursor.isLast()) { // if cursor is at the last raw - move it to the first again (infinite loop)
  50.                 if((dateCompare) && (timeCompare)) {
  51.                     Notify(eventString, eventString); // send notification if current time is the same as the time from db
  52.                     break;
  53.                 }else {
  54.                     cursor.moveToFirst();
  55.                 }
  56.             }
  57.             cursor.moveToNext();
  58.         }while(cursor.moveToNext());
  59.  
  60.         return START_STICKY;
  61.     }
  62.  
  63.     @Override
  64.     public IBinder onBind(Intent intent) {
  65.         return null;
  66.     }
  67.  
  68.     @Override
  69.     public void onDestroy() {
  70.         super.onDestroy();
  71.     }
  72.  
  73.  
  74.  
  75.     private void Notify(String notificationTitle, String bodytext){
  76.  
  77.         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  78.         @SuppressWarnings("deprecation")
  79.  
  80.         Notification notification = new Notification(R.drawable.ic_launcher, "New Message", System.currentTimeMillis());
  81.  
  82.         Intent notificationIntent = new Intent(this, MainActivity.class);
  83.  
  84.         notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
  85.         PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationID, notificationIntent, 0);
  86.  
  87.         notification.setLatestEventInfo(getApplicationContext(), notificationTitle, bodytext, pendingIntent);
  88.         notification.flags |= Notification.FLAG_AUTO_CANCEL;
  89.         notificationManager.notify(notificationID++, notification);
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement