alarm reminder not working

1.7k views Asked by At

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.

4

There are 4 answers

2
archon92 On

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
0
Ramesh On

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

1
CoolKat On

You're never sending the broadcast.

sendbroadcast(myIntent);

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

0
ShibbyUK On

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")