Java 最好使用什么作为SetRepeating()在19 API之后不起作用
我正在做一个应用程序的支持工作,这个应用程序是一个以前的实习学生开发的,它接受用户输入,比如说5。这意味着警报将每5分钟响起一次 我想起了她的应用程序,因为闹钟有自己的想法,而且前后不一致。以下是她正在使用的代码:Java 最好使用什么作为SetRepeating()在19 API之后不起作用,java,android,alarmmanager,repeatingalarm,Java,Android,Alarmmanager,Repeatingalarm,我正在做一个应用程序的支持工作,这个应用程序是一个以前的实习学生开发的,它接受用户输入,比如说5。这意味着警报将每5分钟响起一次 我想起了她的应用程序,因为闹钟有自己的想法,而且前后不一致。以下是她正在使用的代码: alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); //set alert alertDialogBuilder .setTitle("IP
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()
,但在方法中的变量下面有一条红线
有谁能告诉我如何保持变量的一致性
谢谢
setRepeating
和setExact
采用不同数量的变量,因此在不更改代码的情况下不能只使用同一个变量
如果您希望在该准确时间触发某个事件,则需要使用setExact
,每次警报触发时,您需要计算下一次警报自动归档的时间,并再次使用setExact
,为其设置新的触发时间
如果您使用
setRepeating
操作系统可以/将触发时间降低到可以同时触发多个警报以节省电池的时间。您是对的。据
注:自API 19起,所有重复报警均不准确。如果你的
应用程序需要精确的交付时间,然后必须使用一次性交付
准确的警报,按上述每次重新安排。遗产
targetSdkVersion早于API 19的应用程序将
继续保持所有警报,包括重复警报,
被视为精确的
他们建议你做的是使用,而不是只触发一次。
一旦触发报警,请在处理程序或PendingEvent中再次调用set()。我尝试在那里使用set(),但仍然会遇到红线。从我的方法中可以提取哪些变量?你能给我一些关于如何在处理程序或挂起的意图中再次调用set()的代码吗?那么,在第一次调用setExact之后,我该如何再次使用它呢?谢谢回复@tyczjyes每次它触发后,你必须用setExactor设置一个新的警报,然后我将它放置在哪里以确保它重复?如果它无限期地重复,直到停止,该怎么办