I want to reset alarmmanager after phone reboot
This is code for reset alarm after reboot. After phone reboot I query data from SQLi in class AlarmReset and send data to alarmmanager by method au.setAlarm().
public class AlarmReset extends BroadcastReceiver {
SQLiteDatabase db;
DBHelper dbHelper ;
Cursor cursor;
String type_number;
String op_name;
String sNotes;
@Override
public void onReceive(Context context, Intent intent) {
AlarmUtils au = new AlarmUtils();
dbHelper = new DBHelper(context);
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Set the alarm here.
db = dbHelper.getWritableDatabase();
String[] queryColumns = new String[]{DBHelper.COL_DATE,DBHelper.COL_RQCODE , DBHelper.COL_VEHICLE_TYPE, DBHelper.COL_OPTION_NAME};
cursor = db.query(DBHelper.TABLE_NAME, queryColumns,null,null,null,null,null);
while (cursor.moveToNext()) {
au.setAlarm(cursor.getString(0), Integer.parseInt(cursor.getString(1)), "Test !");
}
}
// db.close();
}
}
This is class AlarmUtils the method setAlarm() use for alarm 2 time.
public class AlarmUtils extends AppCompatActivity {
public void setAlarm(String alarmDate, int rq_Code, String sNotes) {
String[] parts = alarmDate.split("-");
String day = parts[0];
String month = parts[1];
String year = parts[2];
Calendar next1 = Calendar.getInstance();
next1.set(Calendar.YEAR, Integer.parseInt(year));//year...
next1.set(Calendar.MONTH, Integer.parseInt(month)-1); //first month is 0!!! January is zero!!!
next1.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day)-1); //1-31
next1.set(Calendar.HOUR_OF_DAY,19); //HOUR
next1.set(Calendar.MINUTE,0); //MIN
next1.set(Calendar.SECOND, 2); //SEC
Calendar next2 = Calendar.getInstance();
next2.set(Calendar.YEAR, Integer.parseInt(year));//year...
next2.set(Calendar.MONTH, Integer.parseInt(month)-1); //first month is 0!!! January is zero!!!
next2.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day)); //1-31
next2.set(Calendar.HOUR_OF_DAY,7); //HOUR
next2.set(Calendar.MINUTE, 0); //MIN
next2.set(Calendar.SECOND, 2); //SEC
Date newDate1 = next1.getTime();
Date newDate2 = next2.getTime();
Intent myIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
myIntent.putExtra("Title", "Title");
myIntent.putExtra("Notes", sNotes);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(), rq_Code, myIntent, 0);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), rq_Code+1, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, next1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS,pendingIntent1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, next2.getTimeInMillis(), DateUtils.DAY_IN_MILLIS,pendingIntent2);
}
}
After I reboot phone and open app it show error like this.
10-29 10:03:23.507 2977-2977/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.max.driver_app, PID: 2977
java.lang.RuntimeException: Unable to start receiver com.example.max.driver_app.AlarmReset: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2438)
at android.app.ActivityThread.access$1700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5059)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:818)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
at com.example.max.driver_app.AlarmUtils.setAlarm(AlarmUtils.java:53)
at com.example.max.driver_app.AlarmReset.onReceive(AlarmReset.java:64)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)
setAlarm(AlarmUtils.java:53) is line
Intent myIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
(AlarmReset.java:64) is line
au.setAlarm(cursor.getString(0), Integer.parseInt(cursor.getString(1)), "Test !");
How to fix it ?
You should avoid instantiate activity class, the applicationContext
will always be null if the activity is not instantiate properly. You could try to change your method to support Context
as parameter:
public static void setAlarm(Context context String alarmDate, int rq_Code, String sNotes) {
// ...
Intent myIntent = new Intent(context, AlarmReceiver.class);
// ...
}
Then pass the context to the utility method in your BroadcastReceiver.onReceive
:
AlarmUtil.setAlarm(context, cursor.getString(0), Integer.parseInt(cursor.getString(1)), "Test !");