I am trying to use BroadcastReceiver and AlarmManager to set a one-shot alarm. I have no idea why isn`t it working. What am I doing wrong? I have no exceptions, no logs about, no suggestions from IDE, everything seems fine, but onReceive method in my BroadcastReceiver is never called.
public void setAlarm(Context mContext) {
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 505151550 ,intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 20);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Log.d("alarm",cal.getTime().toString());
}
I am calling it from the activity:
new MyManager().setAlarm(this);
but I have also tried: new MyManager().setAlarm(MyActivity.this);
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("alarm","got into alarm receiver");
}}
in AndroidManifest.xml :
<receiver android:name="com.example.AlarmReceiver"></receiver>
but I have also tried
<receiver android:name=".AlarmReceiver"></receiver>