I am writing an app that will implement scheduled alarms which has problem in the Receiver Class.
What I want is, firstly, an alarm will be implemented in onCreate Part in the MainActivity.
Then at specific time, when the onRecive void receives the pendingIntent, the onReceive void will call another void in MainActivity. This is done by creating an interface and with empty void so that onReceive can call the method of the same name in MainActivity.
Finally the onReceive void will decide whether a notification shall be sent out depending on the result of the void called from MainActivity through interface.
I tried to referring to this answer and modify it to my script but it returns an error of
Process: com.example.sahelper, PID: 5930
java.lang.RuntimeException: Unable to instantiate receiver com.example.sahelper.alarm.AlarmReceiver: java.lang.InstantiationException: java.lang.Class<com.example.sahelper.alarm.AlarmReceiver> has no zero argument constructor
Here is my MainActivity
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// AlarmController is used to schedule alarm
AlarmController alarmController =
new AlarmController(MainActivity.this, MainActivity.this);
alarmController.new_test_alarm(); // schedule an alarm
}
// This method is supposed to be called by interface
@Override
public void check_duty()
{
// Do something here...
// Do something here...
// Do something here...
}
Here is my AlarmController.java
// Constructor
public AlarmController(Activity activity, Context context)
{
this.myactivity = activity;
this.mycontext = context;
}
public void new_test_alarm()
{
// Some codes here...
// Some codes here...
// Some codes here...
Intent intent = new Intent(myactivity, AlarmReceiver.class);
intent.setAction(SECOND_DEADLINE_ALARM_RECEIVE_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
mycontext,
Integer.parseInt(it.getKey()), // Here is the noti_id
intent,
FLAG_MUTABLE);
AlarmManager alarmManager = (AlarmManager) myactivity.getSystemService
(Context.ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
(System.currentTimeMillis() + 10 * 1000),
pendingIntent);
}
Here is my interface
public interface Receiver_MainActivity_Interface
{
public void check_duty();
}
And finally, here is my receiver class
public class AlarmReceiver extends BroadcastReceiver
{
private Receiver_MainActivity_Interface mylistener;
public AlarmReceiver(Receiver_MainActivity_Interface listener)
{
this.mylistener = listener;
}
@Override
public void onReceive(Context context, Intent intent)
{
if (Objects.equals(intent.getAction(), SECOND_DEADLINE_ALARM_RECEIVE_ACTION))
{
mylistener.check_duty();
// And some codes here continue...
// And some codes here continue...
// And some codes here continue...
}
}
}
I supsect the problem comes from the “Intent intent = new Intent(myactivity, AlarmReceiver.class);” as parameter shall be passed to AlarmReceiver class. But how should I pass the interface as a variable to it?
What shall I do to fix the problem? Any suggestion is appreciated.