Guest User

Untitled

a guest
Sep 18th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. Unable to initate alarm in broadcast receiver onReceive()
  2. public class SmsReceiver extends BroadcastReceiver
  3. {
  4.  
  5. private Context c;
  6.  
  7.  
  8. @Override
  9. public void onReceive(Context context, Intent intent)
  10. {
  11.  
  12. c = context;
  13.  
  14. Bundle extras = intent.getExtras();
  15. String body = "";
  16. String address = "";
  17.  
  18.  
  19.  
  20. if ( extras != null )//if-1
  21. {
  22. // Get received SMS array
  23. Object[] smsExtra = (Object[]) extras.get("pdus");
  24.  
  25.  
  26. for ( int i = 0; i < smsExtra.length; ++i )
  27. {
  28. SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
  29.  
  30. body = sms.getMessageBody().toString();
  31. address = sms.getOriginatingAddress();
  32.  
  33. boolean a = contactExists(context, address);
  34.  
  35.  
  36. if(a == true) // if-2
  37. {
  38. String check = body.split("n")[0].toLowerCase();
  39.  
  40.  
  41. if(check.equals("reminder")) // if-3
  42. {
  43. abortBroadcast();
  44.  
  45. String[] parts = body.split("n");
  46.  
  47. String title = parts[1];
  48. String[] time = parts[2].split(":");
  49. String[] date = parts[3].split("-");
  50. String des = parts[4];
  51.  
  52. int hr = Integer.parseInt(time[0]);
  53. int min = Integer.parseInt(time[1]);
  54. int s = 0;
  55. int day = Integer.parseInt(date[0]);
  56. int mnth = Integer.parseInt(date[1]);
  57. int year = Integer.parseInt(date[2]);
  58.  
  59.  
  60. addRemInDb add = new addRemInDb();
  61. int v = add.insert(title, hr, min, mnth, day, year, des, s);
  62.  
  63. if(v == -1)// if-4
  64. {
  65. Toast.makeText(context, "Title & Time Must Not Be Same",Toast.LENGTH_SHORT).show();
  66. }
  67. else
  68. {
  69.  
  70. Calendar cal = Calendar.getInstance();
  71.  
  72. //Setting alarm to be triggered on specified date and time
  73. cal.set(year, mnth, day, hr, min, s);
  74.  
  75. int requestcode= (int) cal.getTimeInMillis() * (-1);
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. Intent alarmIntent = new Intent(c, AlarmReceiverRem.class);
  84.  
  85. alarmIntent.putExtra("title", title);
  86. alarmIntent.putExtra("note", des);
  87.  
  88.  
  89. PendingIntent sender = PendingIntent.getService(c, requestcode, alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
  90.  
  91.  
  92.  
  93. AlarmManager alarmManager = (AlarmManager)c.getSystemService(Context.ALARM_SERVICE);
  94. alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
  95.  
  96.  
  97.  
  98. add.saveRequestCode(requestcode, title);
  99.  
  100.  
  101. }// end of if-4
  102.  
  103. add.closeDB();
  104.  
  105. }// end of if-3
  106.  
  107. }//end of if-2
  108.  
  109.  
  110. }//end of forloop
  111.  
  112.  
  113. }//end of if-1
  114.  
  115.  
  116.  
  117. }//end of onReceive
  118. }
Add Comment
Please, Sign In to add comment