После перезагрузки телефона Android я сбрасываю AlarmManager по дате запроса из SQLi, чтобы установить AlarmManager.
Это класс AlarmReset после перезагрузки телефона данных запроса этого класса для использования с методом setAlarm ().
public class AlarmReset extends BroadcastReceiver {
SQLiteDatabase db;
DBHelper dbHelper ;
Cursor cursor;
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(context,cursor.getString(0), Integer.parseInt(cursor.getString(1)), "Test !");
}
}
db.close();
}
}
Это класс AlarmUtils для метода создания setAlarm (), он устанавливает alarmmanager.
public class AlarmUtils extends AppCompatActivity {
public void setAlarm(Context context, 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
Intent myIntent = new Intent(context, AlarmReceiver.class);
myIntent.putExtra("Title", "Title");
myIntent.putExtra("Notes", sNotes);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, rq_Code, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, next1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS,pendingIntent1);
}
}
Когда я запускаю этот код, он показывает ошибку, подобную этой.
10-30 00:07:09.889 3303-3303/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.max.driver_app, PID: 3303
java.lang.RuntimeException: Unable to start receiver com.example.max.driver_app.AlarmReset: java.lang.IllegalStateException: System services not available to Activities before onCreate()
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.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4600)
at com.example.max.driver_app.AlarmUtils.setAlarm(AlarmUtils.java:61)
at com.example.max.driver_app.AlarmReset.onReceive(AlarmReset.java:64)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)
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)
setAlarm (AlarmUtils.java:61) - это строка класса AlarmUtils
AlarmManager alarmManager = (AlarmManager)
getSystemService (ALARM_SERVICE);
onReceive (AlarmReset.java:64) является строкой класса AlarmReset
au.setAlarm (контекст, cursor.getString (0),
Integer.parseInt (cursor.getString (1)), "Test!");
Как это исправить?