如何将LocalBroadcastManager用于AlarmManager

时间:2019-05-04 07:51:35

标签: android alarmmanager localbroadcastmanager

我可以在Android上将AlarmManagerLocalBroadcastManager一起使用

我想在12点钟的示例头工作

我写了这段代码,但是没有运行?

代码

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRecurringAlarm(this);

   LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
   new IntentFilter("custom-event-name"));


}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get extra data included in the Intent
        String message = intent.getStringExtra("message");
        Toast.makeText(MainActivity.this, "AlarmManager2", Toast.LENGTH_SHORT).show();
    }
};
private void setRecurringAlarm(Context context) {

    Calendar updateTime = Calendar.getInstance();
    //updateTime.setTimeZone(TimeZone.getTimeZone("GMT+5:00"));
    updateTime.setTimeZone(java.util.TimeZone.getTimeZone("GMT+5:00"));
    updateTime.set(Calendar.HOUR_OF_DAY, 12);
    updateTime.set(Calendar.MINUTE,2);
    updateTime.set(Calendar.SECOND,10);

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent recurringDownload = PendingIntent.getBroadcast(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            recurringDownload);

}

0 个答案:

没有答案