我对Android开发还很陌生,因此我决定创建一个重复警报应用程序,您可以在其中选择周期结束的时间,例如5次警报爆发后。我已经设置了警报以及所有这些,我有一个按钮可以取消警报,但是我不能限制它,因此在达到上述警报数量后它将自动停止。有办法吗?我希望能够在EditText
窗口中写入我想要的突发次数,写入警报之间的延迟,然后按下按钮进行设置。
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private Double delay;
private int howManyTimes;
private EditText remaining;
private EditText iterator;
我想将突发量存储在howManyTimes
中。
我的OnClickListener
看起来像这样(迭代器是我写突发数量的EditText
,其余是我写突发之间的延迟的EditText
:]
public void onClick(View v) {
if (remaining.getText().toString().equals("") || remaining.getText().toString().equals(".")) {
delay = 0.0;
} else {
delay = (60 * 60 * 1000) * Double.parseDouble(remaining.getText().toString());
}
if (iterator.getText().toString().equals("") || iterator.getText().toString().equals(".")) {
howManyTimes = 0;
} else {
howManyTimes = Integer.parseInt(iterator.getText().toString());
}
if (howManyTimes > 0) {
double tmpDelay = delay;
int tmpIterator = howManyTimes;
updateTimeText(tmpIterator, tmpDelay);
startAlarm();
}
}
startAlarm()
看起来像这样:
private void startAlarm() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + delay.longValue(),
delay.longValue(), pendingIntent);
}
这是我的广播接收者:
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationHelper notificationHelper = new NotificationHelper(context);
NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
notificationHelper.getManager().notify(1, nb.build());
}
}
我认为实现此目标的一种方法是将所需的突发量以及到目前为止已过去的突发量存储在持久性内存中,即在您的应用偏好中。这样,即使您在警报之间关闭应用程序,信息也将持续存在。然后仅添加一条if语句,如果经过的突发数量已等于所需的突发总数,就可以取消警报。几秒钟后,我会添加一些代码。
这里您将共享的首选项保存了多少次var,并将经过的首选项的计数器重置为零。
public void onClick(View v) {
if (remaining.getText().toString().equals("") || remaining.getText().toString().equals(".")) {
delay = 0.0;
} else {
delay = (60 * 60 * 1000) * Double.parseDouble(remaining.getText().toString());
}
if (iterator.getText().toString().equals("") || iterator.getText().toString().equals(".")) {
howManyTimes = 0;
} else {
howManyTimes = Integer.parseInt(iterator.getText().toString());
//here you store the amount of desired bursts to persistent memory and reset elapsed bursts to 0
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("desiredBursts",howManyTimes);
editor.putInt("elapseddBursts",0);
editor.apply();
}
if (howManyTimes > 0) {
double tmpDelay = delay;
int tmpIterator = howManyTimes;
updateTimeText(tmpIterator, tmpDelay);
startAlarm();
}
}
然后,您需要在广播接收器中添加以下代码(您的问题中没有包含此代码)。一旦经过的突发次数与期望的突发总数匹配,它将更新经过的突发数量并取消警报。
//this goes in your broadcastreceiver
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(context);
int desiredBursts = sharedPreferences.getInt("desiredBursts", 1);//this will get the desired bursts
int elapsedBursts = sharedPreferences.getInt("elapsedBursts", 0);//this will get the running burst count
if(desiredBursts>elapsedBursts){
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("elapsedBursts",elapsedBursts+1);
editor.apply();
//play actual alarmsound here either with soundpool or mediaplayer
}else{
//cancel the alarm here
}