I have a problem with my BroadcastReceiver. Basically, I have a function in my service, that I want to call every, let’s say, 2 hours. So I used the AlarmManager and Broadcast receiver to do it, however, it looks like the receiver is never called. My code:
in service’s onCreate method:
Intent intent5 = new Intent("MYINTENT");
sender2 = PendingIntent.getBroadcast(this, 192838, intent5, PendingIntent.FLAG_UPDATE_CURRENT);
am2 = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
am2.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), 1000*60*60*2, sender2);
BroadcastReceiver connectionReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
check();
}
};
registerReceiver(connectionReceiver, new IntentFilter("MYINTENT"));
and then there’s the check() method
public static void check(){
//some code
}
I’ve also tried to creat a class extending BroadcastReceiver, registering it in the Manifest etc, and it got called, but then I got a NullPointerException calling the service’s check() metheod from within the BroadcastReceiver.
Go back to that solution, then fix your
NullPointerException. Most likely, Android terminated the process of your app that calledregisterReceiver()(e.g., user pressed HOME and did not return to the process).check()— whatever that is — has to assume that this is a brand-new process. YourNullPointerExceptionprobably comes fromcheck()thinking that something should be initialized, when it is not.