2

我的数据库中有一组提醒(按时间排序)。当我的应用程序启动时,我调用setAlarm. 我需要在onReceive方法中添加代码才能完成这些任务:

  1. 从我的数据库中获取第一个提醒
  2. 获取与提醒相关的延迟
  3. 安排一个新的警报以获得下一个提醒。

我创建了一个简单的 BroadcastReceiver 类:

public class AlarmReceiver extends BroadcastReceiver{
    private static final String DEBUG_TAG= "AlarmReceiver";

    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        Log.d(DEBUG_TAG,"ALARM!!!");
            // --mycode--
    }
}

和活动类:

public class AlarmActivity extends Activity {

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        context = getApplicationContext();
    }


    public void setAlarm(View v){
        Intent intent = new Intent(this,AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ Delay,pendingIntent);
        Log.i("SETTER","Alarm started");

    }

    public void stopAlarm(View v){
        Intent intent = new Intent(this,AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        pendingIntent.cancel();
    }
}

现在,我希望在--mycode--部分中从数据库中获取新的延迟(如果存在),并使用这个新的延迟设置新的警报。如何从 onReceive 方法设置新的 AlarmManager?

4

1 回答 1

3

您可以AlarmManager通过从上下文访问它来进入广播接收器

AlarmManager alarmManager = (AlarmManager)arg0.getSystemService(Context.ALARM_SERVICE);

arg0你的上下文变量在哪里

于 2013-03-11T17:11:02.553 回答