0

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

如何实现年度和每月重复警报?

但是,我遇到了两个错误:

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);

    }

}
4

1 回答 1

1

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

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

PendingIntent sender = PendingIntent.getBroadcast(this,
于 2013-06-14T14:56:57.903 回答