1

I am trying to create reminder using alarm manager. But seems it's not working. I am trying to set multiple reminder using different id but my broadcastreceiver not getting called. I am not seeing any notification or nor any sound.

I have tried 30-40 times.

I am setting date to calender like 25/11/2014

Here is myactivity code which setting alarm notification.

Calendar calendar = Calendar.getInstance();


    calendar.set(Calendar.YEAR, 2014);
    calendar.set(Calendar.MONTH, 11);
    calendar.set(Calendar.DAY_OF_MONTH, 25);

    calendar.set(Calendar.HOUR_OF_DAY, 19);
    calendar.set(Calendar.MINUTE, 30);
    //calendar.set(Calendar.SECOND, 1);

    Intent myIntent = new Intent(CreateReminder.this, MyReceiver.class);
    myIntent.putExtra("reminder_id",reminderid);
    myIntent
    .setAction("com.sandeep.alarm.REMINDER");


    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            CreateReminder.this, reminderid, myIntent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            pendingIntent);   

MyReceiver.class

public class MyReceiver extends BroadcastReceiver {

private Ringtone r;

@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
    int ID=intent.getExtras().getInt("reminder_id");

    Log.i("CreateReminder", "reminder_id:-"+ID);

    int icon = R.drawable.ic_launcher;
      long when = System.currentTimeMillis();
      NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
      Notification notification = new Notification(icon, "reminder", when);
      String title = context.getString(R.string.app_name);
      String subTitle = "Please complete task";
      Intent notificationIntent = new Intent(context, ReminderActivity.class);
      notificationIntent.putExtra("reminder_id", ID);
      PendingIntent intent1 = PendingIntent.getActivity(context, 0,notificationIntent, 0);
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

      notification.setLatestEventInfo(context, title, subTitle, intent1);

      //To play the default sound with your notification:
      //notification.defaults |= Notification.DEFAULT_SOUND;
      notification.flags |= Notification.FLAG_INSISTENT;

      //notification.defaults |= Notification.;
      notificationManager.notify(0, notification);
      Uri notification1 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
      r = RingtoneManager.getRingtone(context, notification1);

      r.play();

      new Handler().postDelayed(new Runnable(){
            public void run() {
                r.stop();
            }
        }, 10000);

  }
}    

I am registering my receiver in AndroidManifest.xml with permissions.

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.sandip.remindme.CreateReminder"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.sandip.remindme.ReminderActivity"></activity>

    <receiver android:name="com.sandip.remindme.MyReceiver"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.sandeep.alarm.REMINDER" />
            </intent-filter>
    </receiver>
</application>    

I don't understand why it's not working. Please give me some hints or reference.

Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
  • 1
    Are you sure the Calendar is set correctly? Remember, months are zero-based; i.e., `Calendar.JANUARY==0`. Also, if you're trying to set it for the current time, you don't need the `calendar.set()` calls. `Calendar.getInstance()` returns an instance with the current time already set. – Mike M. Nov 22 '14 at 19:17
  • I would definitely discard all the Calendar thing (you might have TimeZone issues too) and just use System.currentTimeMillis() + (30 *1000) and expect the alarm to go off in 30 seconds. If it doesn't then you have a problem with the wiring of all components, if it does, then your Calendar logic is not really working. – Robert Estivill Nov 25 '14 at 23:42
  • @Sandip I ran your code with the `calendar.set()` methods removed for testing, and it fires immediately as expected. Unless the time is somehow being set incorrectly, the only other thing I can think of is that your Receiver isn't in the correct folder. – Mike M. Nov 27 '14 at 23:44
  • @MikeM.I have put Receiver in same folder. So what i am doing. I am getting time from time picker and setting hours and minutes to calender. and accepting simple date from user like 12/12/2014 from user and setting it to calender. So am I doing something wrong in that? – Sandip Armal Patil Nov 28 '14 at 06:23
  • @Sandip Yeah, without seeing the code you're using to determine the date and time, I can only assume that something might be off there. As I mentioned, remember that `JANUARY==0`. Also, make sure you don't get the month/day mixed up if the user is entering the date manually and you're parsing the String. – Mike M. Nov 28 '14 at 10:46
  • yes @MikeM. you right. I am starting January==1 so it's going 1 month forward. Thanks for your reply. :-) – Sandip Armal Patil Nov 29 '14 at 06:12
  • refer this http://stackoverflow.com/questions/5976098/how-to-set-a-reminder-in-android ,its help full for me in past . – Radhey Dec 01 '14 at 09:12

4 Answers4

1

You are not sending your broadcast after you set your action in your intent.

 Intent myIntent = new Intent(CreateReminder.this, MyReceiver.class);
    myIntent.putExtra("reminder_id",reminderid);
    myIntent
    .setAction("com.sandeep.alarm.REMINDER");
sendbroadcast(myIntent);//YOU FORGOT TO ADD THIS
archon92
  • 447
  • 3
  • 13
  • 1
    That Intent is used to create a PendingIntent to be passed to AlarmManager. It is not meant to be broadcast at that point. – Mike M. Dec 02 '14 at 09:43
  • yes @MikeM.! problem was with setting incorrect date to reminder. I am setting `January=1` instead of `January=0`. @archon92 it's not related to sendBroadcast(intent). – Sandip Armal Patil Dec 02 '14 at 09:45
0

You're never sending the broadcast.

sendbroadcast(myIntent);

Add that directly after you do .setAction, and the code should work just fine.

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
CoolKat
  • 112
  • 1
  • 12
  • 1
    That Intent is used to create a PendingIntent to be passed to AlarmManager. It is not meant to be broadcast at that point. – Mike M. Dec 02 '14 at 09:43
0

I'm also working on Alarm remainder and I've implemented successfully in my app. you can download the example code in the following link. I hope it helps you. Please note that alarm takes 30secs to 1 min to call broadcast receiver. i.e., if you set alarm at 3:30:00pm then broad cast receiver will be called at around 3:30:30pm.

https://www.dropbox.com/s/t2m5ph8s8f17v6m/AndroidAlarmManager.zip?dl=0

Thanks

Ramesh

Ramesh
  • 1,252
  • 3
  • 12
  • 30
0

I have compared your code to mine and have a couple of things for you to try...

Is com.sandeep.alarm a real namespace? The activities within your app have the differing namespace com.sandip.remindme so you could use this ("com.sandip.remindme.REMINDER") for your action name.

Your intent is created with a specific context and class target. Try constructing it with just the action name, then you will know if the action name is the issue:

Intent myIntent = new Intent("com.sandip.remindme.REMINDER")
ShibbyUK
  • 1,501
  • 9
  • 12