Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyService extends Service {
- private DBHelper dbHelper;
- private SQLiteDatabase db;
- private int notificationID = 100;
- @Override
- public void onCreate() {
- Log.i("myLOgs", "Service: onCreate()");
- super.onCreate();
- dbHelper = new DBHelper(this);
- db = dbHelper.getWritableDatabase();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.i("myLOgs", "Service: onStartCommand()");
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Date date = new Date();
- String currentDateString = dateFormat.format(date);
- Log.i("myLOgs", currentDateString);
- SimpleDateFormat timeFormat = new SimpleDateFormat("HH-mm");
- Date time = new Date();
- String currentTimeString = timeFormat.format(time);
- Log.i("myLogs", currentTimeString);
- String[] columns = {DBHelper.DATE, DBHelper.TIME, DBHelper.EVENT};
- Cursor cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
- cursor.moveToFirst(); // move cursor to the first raw
- do{
- //get all info from database
- String dateString = cursor.getString(cursor.getColumnIndex(DBHelper.DATE));
- String timeString = cursor.getString(cursor.getColumnIndex(DBHelper.TIME));
- String eventString = cursor.getString(cursor.getColumnIndex(DBHelper.EVENT));
- // Log.i("myLogs", dateString);
- // Log.i("myLOgs", timeString);
- // Log.i("myLogs", eventString);
- boolean dateCompare = currentDateString.equals(dateString);
- boolean timeCompare = currentTimeString.equals(timeString);
- if((dateCompare) && (timeCompare)) {
- Notify(eventString, eventString); // send notification if current time is the same as the time from db
- break; //temporary
- }
- if(cursor.isLast()) { // if cursor is at the last raw - move it to the first again (infinite loop)
- if((dateCompare) && (timeCompare)) {
- Notify(eventString, eventString); // send notification if current time is the same as the time from db
- break;
- }else {
- cursor.moveToFirst();
- }
- }
- cursor.moveToNext();
- }while(cursor.moveToNext());
- return START_STICKY;
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- }
- private void Notify(String notificationTitle, String bodytext){
- NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- @SuppressWarnings("deprecation")
- Notification notification = new Notification(R.drawable.ic_launcher, "New Message", System.currentTimeMillis());
- Intent notificationIntent = new Intent(this, MainActivity.class);
- notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationID, notificationIntent, 0);
- notification.setLatestEventInfo(getApplicationContext(), notificationTitle, bodytext, pendingIntent);
- notification.flags |= Notification.FLAG_AUTO_CANCEL;
- notificationManager.notify(notificationID++, notification);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement