Advertisement
jasperlow

Untitled

Jun 29th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. package com.sifb.sme.ewallet.net.reminder;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlarmManager;
  5. import android.app.Notification;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.os.Bundle;
  11. import android.os.PowerManager;
  12. import android.support.v4.app.NotificationCompat;
  13.  
  14. import com.sifb.sme.ewallet.R;
  15. import com.sifb.sme.ewallet.base.MyActivity;
  16. import com.sifb.sme.ewallet.net.main.Activity_Main;
  17. import com.sifb.sme.ewallet.net.util.MyLogsUtil;
  18.  
  19. public class ReminderGenerator {
  20.  
  21. private static final String TAG = MyActivity.PROJ + "ReminderGenerator";
  22. public static final String PARC_ALARM = "PARC_ALARM";
  23.  
  24. public static AlarmParcel setReminderInfo(int aID, String aType, String aTitle, String aImgStr) {
  25. MyLogsUtil.loggi(TAG, "setReminderInfo");
  26. AlarmParcel aAlarm = new AlarmParcel();
  27. aAlarm.setaID(aID);
  28. aAlarm.setaType(aType);
  29. aAlarm.setaTitle(aTitle);
  30. aAlarm.setaImgStr(aImgStr);
  31. return aAlarm;
  32. }
  33.  
  34. public static void setReminderAlarm(Activity act, Long aTime, AlarmParcel iAlarm) {
  35. MyLogsUtil.loggi(TAG, "setReminderAlarm");
  36. Bundle iBundle = new Bundle();
  37. iBundle.putParcelable(PARC_ALARM, iAlarm);
  38. Intent alarmIntent = new Intent(act, AlarmReceiver.class);
  39. alarmIntent.putExtras(iBundle);
  40.  
  41. PendingIntent pendingIntent = PendingIntent.getBroadcast(act, iAlarm.getaID(), alarmIntent, 0);
  42.  
  43. AlarmManager manager = (AlarmManager) act.getSystemService(Context.ALARM_SERVICE);
  44. manager.set(AlarmManager.RTC_WAKEUP, aTime, pendingIntent);
  45. }
  46.  
  47. public static void delReminderAlarm(Activity act, AlarmParcel iAlarm) {
  48. MyLogsUtil.loggi(TAG, "delReminderAlarm");
  49. Bundle iBundle = new Bundle();
  50. iBundle.putParcelable(PARC_ALARM, iAlarm);
  51. Intent alarmIntent = new Intent(act, AlarmReceiver.class);
  52. alarmIntent.putExtras(iBundle);
  53.  
  54. PendingIntent pendingIntent = PendingIntent.getBroadcast(act, iAlarm.getaID(), alarmIntent, 0);
  55.  
  56. AlarmManager manager = (AlarmManager) act.getSystemService(Context.ALARM_SERVICE);
  57. manager.cancel(pendingIntent);
  58.  
  59. }
  60.  
  61. public static void generateNotification(Context ctx, Bundle iBundle) {
  62. MyLogsUtil.loggi(TAG, "generateNotification");
  63. AlarmParcel nAlarm = (AlarmParcel) iBundle.getParcelable(PARC_ALARM);
  64.  
  65. if (nAlarm != null) {
  66. int nID = nAlarm.getaID();
  67. String nTitle = nAlarm.getaTitle();
  68. String nLabel = ctx.getResources().getString(R.string.mysnapshot_notification_lb);
  69. String nContent = ctx.getResources().getString(R.string.mysnapshot_notification_ct);
  70.  
  71. Intent nIntent = new Intent(ctx, Activity_Main.class);
  72. nIntent.putExtras(iBundle);
  73. nIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
  74.  
  75. PendingIntent piNotification = PendingIntent.getActivity(ctx, nID, nIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  76.  
  77. NotificationCompat.Builder bNotification = new NotificationCompat.Builder(ctx);
  78. bNotification.setSmallIcon(R.drawable.gen_ic_launcher).setContentTitle(nLabel + ": " + nTitle).setContentText(nContent).setContentIntent(piNotification);
  79. bNotification.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
  80.  
  81. NotificationManager mNotification = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
  82. mNotification.notify(nID, bNotification.build());
  83.  
  84. PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
  85. PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "ExpiryWakeLock");
  86. // This will make the screen and power stay on
  87. // This will release the wakelook after 1000 ms
  88. wakeLock.acquire(3000);
  89. // Alternative you can request and / or release the wakelook via:
  90. // wakeLock.acquire(); wakeLock.release();
  91.  
  92. } else {
  93. MyLogsUtil.loggi(TAG, "generateNotification failed: parcelable null");
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement