我在alarmmanager上找到了很多答案,设置了重复闹钟,但我想设置闹钟,在我点击按钮2分钟后设置闹钟,它只会一次,我不想重复。
请帮忙
答案 0 :(得分:1)
API< 19
从API 19(KITKAT)开始,警报传递不准确:操作系统将移动警报以最小化唤醒和电池使用。有新的API支持需要严格交付保证的应用程序;请参阅setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。 targetSdkVersion早于API 19的应用程序将继续查看之前在请求时准确传递所有警报的行为。
参考:https://developer.android.com/reference/android/app/AlarmManager.html
答案 1 :(得分:0)
试试这个,我希望它对你有用
int milliseconds = 120000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
yourMethod(); // this method will be called after 2 minutes
}
},milliseconds);
答案 2 :(得分:0)
您可以使用此功能,仅在2分钟后通知一次,您也可以将其更改为秒或小时。
JAVA
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 2); //Minutes
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, MyBroadCastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);
科特琳
val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.add(Calendar.MINUTE, 2)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager?
val alarmIntent = Intent(context, AlarmReceiver::class.java)
alarmIntent.action = "FOO_ACTION"
alarmIntent.putExtra("alarm", "A")
val pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0)
alarmManager!!.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)