gpt4 book ai didi

android - 警报管理器重复

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:52 32 4
gpt4 key购买 nike

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

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

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

这是报警服务:

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

你能帮帮我吗?

最佳答案

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 second

示例:

1year = 365(day)* 24(hour)* 60(min)* 60(second)* 1000(sec -> milisecond);
// leap year 366day

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

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {

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

}

更新:注意:从 API 19 (Build.VERSION_CODES.KITKAT) 开始,警报传送不准确:操作系统将转移警报以尽量减少唤醒和电池使用。有新的 API 来支持需要严格交付保证的应用程序;参见 setWindow(int, long, long, PendingIntent) 和 setExact(int, long, PendingIntent)。 targetSdkVersion 早于 API 19 的应用程序将继续看到以前的行为,即所有警报都在请求时准确传递。 reference

我推荐使用 JobScheduler适用于安卓 21 或 Firebase JobDispatcher对于旧设备而不是警报管理器

更新:Google 已将 WorkManager 作为 JetPack 的一部分发布 Schedule tasks with WorkManager

关于android - 警报管理器重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11123543/

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