我设置一个60秒的警报,该警报应该在60秒后触发,但是,每当我通过按钮点击设置时间时,我的onreceive会被触发,并且在60秒后也会被触发。我该如何解决这个问题?
这就是我设置闹钟的方式
// ENABLE Receiver
ComponentName receiver = new ComponentName(MainActivity.this,
GhostTimeBombReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(
this,
"Enabled broadcast receiver, Alarm Manager, task will be repeated",
Toast.LENGTH_LONG).show();
// --//
// disable any existing alarm
Intent intent = new Intent(this, GhostTimeBombReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
// disable any existing alarm
// ENABLE Alarm Manager
AlarmManager am = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
long recurring = (1 * 1000 * 60); // 60 seconds
am.setRepeating(AlarmManager.RTC, Calendar.getInstance()
.getTimeInMillis(), recurring, sender);
// --//
这是我的接收器
public class GhostTimeBombReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "this toast is called via Receiver",
Toast.LENGTH_LONG).show();
// ... do what you need to do here...
}
}
答案 0 :(得分:0)
替换:
am.setRepeating(AlarmManager.RTC, Calendar.getInstance()
.getTimeInMillis(), recurring, sender);
使用:
am.setRepeating(AlarmManager.RTC, Calendar.getInstance()
.getTimeInMillis()+recurring, recurring, sender);