有没有办法在 Android 中安排重复警报的结束?
Is there a way to schedule the end of the repeating alarm in Android?
我是 Android 开发的新手,我决定创建一个重复闹钟应用程序,您可以在其中选择循环结束的时间,例如 5 次闹钟爆发后。我已经设置了闹钟和所有这些,我有一个按钮可以取消闹钟,但我不能限制它,所以它会在上述数量的闹钟爆发后自动停止。有办法吗?我希望可以在EditText
window里面写出我想要多少个burst,写出闹钟之间的延迟然后按下按钮来设置。
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 到 shared prefs 并将经过的计数器重置为零。
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
}
我是 Android 开发的新手,我决定创建一个重复闹钟应用程序,您可以在其中选择循环结束的时间,例如 5 次闹钟爆发后。我已经设置了闹钟和所有这些,我有一个按钮可以取消闹钟,但我不能限制它,所以它会在上述数量的闹钟爆发后自动停止。有办法吗?我希望可以在EditText
window里面写出我想要多少个burst,写出闹钟之间的延迟然后按下按钮来设置。
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 到 shared prefs 并将经过的计数器重置为零。
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
}