gpt4 book ai didi

java - 上下文无法解析

转载 作者:行者123 更新时间:2023-12-01 14:27:16 25 4
gpt4 key购买 nike

我正在尝试使用此处描述的方法来安排每月警报:

How to implement yearly and monthly repeating alarms?

但是我收到两个错误:

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

PendingIntent sender = PendingIntent.getBroadcast(context,

声明“上下文无法解析”和“上下文无法解析为变量”

...有关如何解决此问题的任何建议?

 public class Alarm extends Service {

// compat to support older devices
@Override
public void onStart(Intent intent, int startId) {
onStartCommand(intent, 0, startId);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// your method to check if an alarm must be fired today

PendingIntent sender = PendingIntent.getBroadcast(context,
Integer.parseInt(Long.toString(id)), intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(calendar.getTimeInMillis());
calendar.add(Calendar.SECOND, 30);

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

calendar.add(Calendar.MONTH, 1);

am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

// reschedule me to check again tomorrow
Intent serviceIntent = new Intent(Alarm.this, Alarm.class);
PendingIntent restartServiceIntent = PendingIntent.getService(
Alarm.this, 0, serviceIntent, 0);
AlarmManager alarms = (AlarmManager) getSystemService(ALARM_SERVICE);
// cancel previous alarm
alarms.cancel(restartServiceIntent);
// schedule alarm for today + 1 day
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);

// schedule the alarm
alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
restartServiceIntent);
}

@Override
public void onCreate() {

// TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

@Override
public boolean onUnbind(Intent intent) {

// TODO Auto-generated method stub

return super.onUnbind(intent);

}

}

最佳答案

服务是一个上下文,所以试试这个代码:

AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

PendingIntent sender = PendingIntent.getBroadcast(this,

关于java - 上下文无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17110496/

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