gpt4 book ai didi

java - AlarmManager 重置显示错误 在 onCreate() 之前系统服务不可用于 Activity

转载 作者:行者123 更新时间:2023-12-02 10:39:16 24 4
gpt4 key购买 nike

Android 手机重新启动后,我通过 SQLi 的查询日期重置 Alarmmanager 以设置 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(context,cursor.getString(0), Integer.parseInt(cursor.getString(1)), "Test !");

如何解决?

最佳答案

只需使用上下文初始化 AlarmManager,以便进行更改
AlarmManager AlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

to:   

AlarmManager AlarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

关于java - AlarmManager 重置显示错误 在 onCreate() 之前系统服务不可用于 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53050698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com