Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. package app.gamatechno.com.alarmmanager;
  2.  
  3. import android.app.AlarmManager;
  4. import android.app.Notification;
  5. import android.app.NotificationChannel;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.BroadcastReceiver;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.media.RingtoneManager;
  12. import android.net.Uri;
  13. import android.os.Build;
  14. import android.support.v4.app.NotificationCompat;
  15. import android.support.v4.content.ContextCompat;
  16. import android.util.Log;
  17. import android.widget.Toast;
  18.  
  19. import java.text.DateFormat;
  20. import java.text.ParseException;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Calendar;
  23. import java.util.Locale;
  24.  
  25. public class AlarmReceiver extends BroadcastReceiver {
  26.  
  27. public static final String TYPE_ONE_TIME = "OneTimeAlarm";
  28. public static final String EXTRA_MESSAGE = "message";
  29. public static final String EXTRA_TYPE = "type";
  30. private final int ID_ONETIME = 100;
  31.  
  32. public AlarmReceiver() {
  33. }
  34.  
  35. @Override
  36. public void onReceive(Context context, Intent intent) {
  37. String type = intent.getStringExtra(EXTRA_TYPE);
  38. String message = intent.getStringExtra(EXTRA_MESSAGE);
  39. String title = type.equalsIgnoreCase(TYPE_ONE_TIME) ? TYPE_ONE_TIME : "-";
  40. int notifId = type.equalsIgnoreCase(TYPE_ONE_TIME) ? ID_ONETIME : 0;
  41. showToast(context, title, message);
  42. showAlarmNotification(context, title, message, notifId);
  43. }
  44.  
  45. private void showToast(Context context, String title, String message) {
  46. Toast.makeText(context, title + " : " + message, Toast.LENGTH_LONG).show();
  47. }
  48.  
  49. public void setOneTimeAlarm(Context context, String type, String date, String time, String message) {
  50. String DATE_FORMAT = "yyyy-MM-dd";
  51. String TIME_FORMAT = "HH:mm";
  52. if (isDateInvalid(date, DATE_FORMAT) || isDateInvalid(time, TIME_FORMAT)) return;
  53. AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  54. Intent intent = new Intent(context, AlarmReceiver.class);
  55. intent.putExtra(EXTRA_MESSAGE, message);
  56. intent.putExtra(EXTRA_TYPE, type);
  57. Log.e("ONE TIME", date + " " + time);
  58. String dateArray[] = date.split("-");
  59. String timeArray[] = time.split(":");
  60. Calendar calendar = Calendar.getInstance();
  61. calendar.set(Calendar.YEAR, Integer.parseInt(dateArray[0]));
  62. calendar.set(Calendar.MONTH, Integer.parseInt(dateArray[1]) - 1);
  63. calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateArray[2]));
  64. calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeArray[0]));
  65. calendar.set(Calendar.MINUTE, Integer.parseInt(timeArray[1]));
  66. calendar.set(Calendar.SECOND, 0);
  67. PendingIntent pendingIntent = PendingIntent.getBroadcast(context, ID_ONETIME, intent, 0);
  68. if (alarmManager != null) {
  69. alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
  70. }
  71. Toast.makeText(context, "One time alarm set up", Toast.LENGTH_SHORT).show();
  72. }
  73.  
  74. public boolean isDateInvalid(String date, String format) {
  75. try {
  76. DateFormat df = new SimpleDateFormat(format, Locale.getDefault());
  77. df.setLenient(false);
  78. df.parse(date);
  79. return false;
  80. } catch (ParseException e) {
  81. return true;
  82. }
  83. }
  84.  
  85. private void showAlarmNotification(Context context, String title, String message, int notifId) {
  86. String CHANNEL_ID = "Channel_1";
  87. String CHANNEL_NAME = "AlarmManager channel";
  88. NotificationManager notificationManagerCompat = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  89. Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  90. NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
  91. .setSmallIcon(R.drawable.ic_access_time_black)
  92. .setContentTitle(title)
  93. .setContentText(message)
  94. .setColor(ContextCompat.getColor(context, android.R.color.transparent))
  95. .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
  96. .setSound(alarmSound);
  97. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  98. NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
  99. CHANNEL_NAME,
  100. NotificationManager.IMPORTANCE_DEFAULT);
  101. channel.enableVibration(true);
  102. channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
  103. builder.setChannelId(CHANNEL_ID);
  104. if (notificationManagerCompat != null) {
  105. notificationManagerCompat.createNotificationChannel(channel);
  106. }
  107. }
  108. Notification notification = builder.build();
  109. if (notificationManagerCompat != null) {
  110. notificationManagerCompat.notify(notifId, notification);
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement