Guest User

Untitled

a guest
Jan 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. private void saveState() {
  2.  
  3. DatabaseHelper myDbHelper = new DatabaseHelper(ReminderEditActivity.this);
  4. try {
  5. myDbHelper.createDataBase();
  6. } catch (IOException ioe) {
  7. throw new Error("Unable to create database");
  8. }
  9. try {
  10. myDbHelper.openDataBase();
  11. }catch(SQLException sqle){
  12. throw sqle;
  13. }
  14. c=myDbHelper.query("tblmain", null, null, null, null,null, null);
  15. if(c.moveToFirst())
  16. {
  17. do {
  18. mRowId = c.getLong(0);
  19. String datetime = c.getString(8);
  20.  
  21. SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
  22. Date date = null;
  23. try {
  24. date = dateTimeFormat.parse(datetime);
  25. mCalendar.setTime(date);
  26. } catch (ParseException e) {
  27. Log.e("ReminderEditActivity", e.getMessage(), e);
  28. }
  29. mCalendar.setTime(date);
  30.  
  31. new ReminderManager(this).setReminder(mRowId, mCalendar);
  32. } while (c.moveToNext());
  33. }
  34. }
  35.  
  36. public class ReminderManager {
  37.  
  38. private Context mContext;
  39. private AlarmManager mAlarmManager;
  40.  
  41. public ReminderManager(Context context) {
  42. mContext = context;
  43. mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  44. }
  45.  
  46. public void setReminder(Long taskId, Calendar when) {
  47.  
  48. Intent i = new Intent(mContext, OnAlarmReceiver.class);
  49. i.putExtra(RemindersDbAdapter.KEY_eventid, (long)taskId);
  50.  
  51. PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
  52.  
  53. mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
  54. }
  55. }
  56.  
  57. public class OnAlarmReceiver extends BroadcastReceiver {
  58.  
  59. private static final String TAG = ComponentInfo.class.getCanonicalName();
  60.  
  61.  
  62. @Override
  63. public void onReceive(Context context, Intent intent) {
  64. Log.d(TAG, "Received wake up from alarm manager.");
  65.  
  66. long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_eventid);
  67.  
  68. WakeReminderIntentService.acquireStaticLock(context);
  69.  
  70. Intent i = new Intent(context, ReminderService.class);
  71. i.putExtra(RemindersDbAdapter.KEY_eventid, rowid);
  72. context.startService(i);
  73.  
  74. }
  75. }
  76.  
  77. public class ReminderService extends WakeReminderIntentService {
  78.  
  79. public ReminderService() {
  80. super("ReminderService");
  81. }
  82.  
  83. @Override
  84. void doReminderWork(Intent intent) {
  85. Log.d("ReminderService", "Doing work.");
  86. Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_eventid);
  87.  
  88. NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  89.  
  90. Intent notificationIntent = new Intent(this, ReminderEditActivity.class);
  91. notificationIntent.putExtra(RemindersDbAdapter.KEY_eventid, rowId);
  92.  
  93. PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
  94.  
  95. Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis());
  96. note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi);
  97. note.defaults |= Notification.DEFAULT_SOUND;
  98. note.flags |= Notification.FLAG_AUTO_CANCEL;
  99.  
  100. // An issue could occur if user ever enters over 2,147,483,647 tasks. (Max int value).
  101. // I highly doubt this will ever happen. But is good to note.
  102. int id = (int)((long)rowId);
  103. mgr.notify(id, note);
  104. }
  105. }
  106.  
  107. String datetime = c.getString(8);
Add Comment
Please, Sign In to add comment