【发布时间】:2018-05-08 15:01:51
【问题描述】:
我正在为以前的实习学生开发的应用程序做一些支持工作,该应用程序接受用户输入,比如 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(),但在方法内的变量下方出现了一条红线。
谁能告诉我如何在保持变量的同时保持一致性?
谢谢!
【问题讨论】:
标签: java android alarmmanager repeatingalarm