简体   繁体   中英

AlarmManager is unable to call my BroadcastReceiver

I am trying to make a reminder application but my BroadcastReceiver is not getting any call from AlarmManager.

public void setUpReminder(Reminder reminder){//Method to setup Reminder
    //String content = dbhandler.getContent(reminder.getDeadline());
    String content = dbhandler.getContent(reminder.getName().toString());
    System.out.println(date_string);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this,Receiver.class);
    intent.putExtra("content",content);

    PendingIntent intent2 = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Integer.valueOf(date_string.substring(0,4)),Integer.valueOf(date_string.substring(5,7)), Integer.valueOf(date_string.substring(8,10)),Integer.valueOf(date_string.substring(11,13)),Integer.valueOf(date_string.substring(14,16)));
    //calendar.set(2019,Integer.valueOf(date_string.substring(5,7)), Integer.valueOf(date_string.substring(8,10)),Integer.valueOf(date_string.substring(11,13)),Integer.valueOf(date_string.substring(14,16)));
    alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),intent2);
}

Here is the code of BroadcastReceiver

package com.example.reminderapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String content = intent.getStringExtra("content");
    System.out.println(content);
    Intent callRinger = new Intent(context.getApplicationContext(),ReminderActivity.class);
    callRinger.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    callRinger.putExtra("content",content);
    context.startActivity(callRinger);
    }
}

I have even added Receiver in my AndroidManifest.xml

 <receiver android:process=":remote"  android:name=".Receiver" />

Please tell me what I am doing wrong.

When you do this:

calendar.set(Integer.valueOf(date_string.substring(0,4)),
    Integer.valueOf(date_string.substring(5,7)),
    Integer.valueOf(date_string.substring(8,10)),
    Integer.valueOf(date_string.substring(11,13)),
    Integer.valueOf(date_string.substring(14,16)));

I am assuming that your date_string is something like 2019-12-18T08:55 . If this is the case, the reason that your alarm isn't going off is due to the month field. The Calendar class expects the month field to be zero-based (ie: values 0-11), but your date_string has the month field as one-based (ie: 1-12).

To test this theory, try hardcoding the values and testing if the alarm triggers, for example:

calendar.set(2019,11,31,09,05)

Tnese values would set the Calendar to 2019.12.31 at 09:05 (notice the month field is one less than the actual month number)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM