首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AlarmManager重复

AlarmManager重复
EN

Stack Overflow用户
提问于 2012-06-21 00:01:57
回答 2查看 7.6K关注 0票数 5

我有一个闹钟,它在特定的时间发送通知(如果在那一天有联系人的生日)。

我需要的是,当我设置闹钟时,它应该在每年的同一天和同一时间重复。我怎么能这么做呢?

代码语言:javascript
复制
Intent myIntent = new Intent(Main.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(Main.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(2012,5,20,18,40);

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60*60*24*365*1000, pendingIntent);

这是AlarmService:

代码语言:javascript
复制
public class MyAlarmService extends Service {
  static final int uniqueID = 1394885;
  private PendingIntent pendingIntent;

  @Override
  public void onCreate() {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();

  }

  @Override
  public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
    return null;
  }

  @Override
  public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
  }

  @Override
  public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub

  }

  @Override
  public boolean onUnbind(Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
    return super.onUnbind(intent);
  }
}

你能帮帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-31 12:27:16

代码语言:javascript
复制
Intent intent = new Intent(this, MyReceiver.class);
intent.putExtra("key", "Alert");
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();

// Calendar.set(int year, int month, int day, int hourOfDay, int minute, int second)
calendar.set(2013, Calendar.OCTOBER, 13, 18, 55, 40);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);

此参数是报警后续重复之间的时间间隔,单位为毫秒:

5*1000 =5秒

示例:

1year =365(天)*24(小时)*60(分钟)*60(秒)*1000(秒->毫秒);

//闰年366天

month值是从0开始的,因此使用像Calendar.OCTOBER这样的常量可能会更清楚

MyReceiver.java

代码语言:javascript
复制
public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
       Toast.makeText(context,intent.getStringExtra("key"),Toast.LENGTH_SHORT).show();
  }

}

update:注意:从API19 (Build.VERSION_CODES.KITKAT)开始,警报发送是不准确的:操作系统将移动警报,以最大限度地减少唤醒和电池消耗。有一些新的API可以支持需要严格交付保证的应用程序;请参阅setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。targetSdkVersion早于API19的应用程序将继续看到之前的行为,即所有告警都会在请求时准确传递。reference

我建议使用适用于安卓21的JobScheduler或适用于旧设备的Firebase JobDispatcher,而不是警报管理器

更新:谷歌发布了WorkManager作为JetPack Schedule tasks with WorkManager的一部分

票数 8
EN

Stack Overflow用户

发布于 2012-06-21 00:11:10

使用alarmManager.set()设置闹钟

代码语言:javascript
复制
Intent myIntent = new Intent(Main.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(Main.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(2012,5,20,18,40);

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);  

而且,当闹钟响起时,从MyAlarmService设置下一年的闹钟。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11123543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档