java最适合用作SetRepeating()的内容在19 API之后不起作用
我正在做一个应用程序的支持工作,这个应用程序是一个以前的实习学生开发的,它接受用户输入,比如说5。这意味着警报将每5分钟响起一次
我想起了她的应用程序,因为闹钟有自己的想法,而且前后不一致。以下是她正在使用的代码:
alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
//set alert
alertDialogBuilder
.setTitle("IP Check frequency: " + time.getText() + " minutes")
.setMessage("Processing commenced at \n" + startTime.getText())
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (currentTime.after(alarmTime)) {
Toast.makeText(MainActivity.this, "Missed first alert", Toast.LENGTH_LONG).show();
}
intent1 = new Intent(MainActivity.this, MyBroadcastReceiver.class);
i = Integer.parseInt(time.getText().toString());
scTime2 = (i * 60 * 1000); //5 minutes before set time
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP, timeCommenced, scTime2, pendingIntent);
Toast.makeText(MainActivity.this, "Alert Set", Toast.LENGTH_SHORT).show();
stopped.setVisibility(View.VISIBLE);
commenced1.setVisibility(View.GONE);
//Change editText to TextView
time.setVisibility(View.GONE);
timeText.setVisibility(View.VISIBLE);
timeText.setText(time.getText().toString());
processingText.setText(R.string.processing_commenced);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
我可以看到她正在使用SetRepeating()
,我已经从api级别19中了解到,这可能就是问题所在
我尝试过使用SetExact()
,但是在方法中的变量下面有一条红线
有谁能告诉我如何保持变量的一致性
谢谢
# 1 楼答案
setRepeating
和setExact
采用不同数量的变量,因此在不更改代码的情况下,不能只使用同一个变量如果您希望在该确切时间触发某个事件,则需要使用
setExact
,并且每次警报触发时,您需要计算下一次警报自动归档的时间,并再次使用setExact
和新的触发时间如果您使用
setRepeating
,操作系统可以/将会将触发时间减少到可以同时触发多个警报以节省电池的时间# 2 楼答案
你说得对。根据https://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,%20long,%20long,%20android.app.PendingIntent)
他们建议您使用set()而不是只触发一次的setRepeating()。 触发报警后,在处理程序或PendingEvent内再次调用set()