gpt4 book ai didi

android - 如何暂停和恢复 Android 闹钟?

转载 作者:太空宇宙 更新时间:2023-11-03 11:17:44 27 4
gpt4 key购买 nike

我正在开发一个闹钟,它会按规定的时间间隔振动。因此,如果我选择 5 秒,应用程序将每隔 5 秒振动一次。但我需要停止、暂停和恢复应用程序。

这是我的课:

public class AndroidAlarmService extends Activity {

private PendingIntent pendingIntent;
private Chronometer chronometer;
private Intent myIntent;
AlarmManager alarmManager;
Calendar calendar;
private long mTimeWhenStopped;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStart = (Button) findViewById(R.id.startalarm);
Button buttonStop = (Button) findViewById(R.id.stopalarm);
Button buttonPause = (Button) findViewById(R.id.pausealarm);
Button buttonResume = (Button) findViewById(R.id.resumealarm);

chronometer = ((Chronometer) findViewById(R.id.clock_time));

myIntent = new Intent(AndroidAlarmService.this,
MyAlarmService.class);
pendingIntent = PendingIntent.getService(
AndroidAlarmService.this, 0, myIntent, 0);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
calendar = Calendar.getInstance();

我用这个方法启动它:

buttonStart.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View arg0) {

int initialValue = 0;

chronometer.setBase(SystemClock.elapsedRealtime() + initialValue);
chronometer.start();

calendar.setTimeInMillis(System.currentTimeMillis());

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 5 * 1000, pendingIntent);

Toast.makeText(AndroidAlarmService.this, "Start Alarm",
Toast.LENGTH_LONG).show();
}
});

这用于暂停:

    buttonPause.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View arg0) {
stopRunning();

mTimeWhenStopped = chronometer.getBase() - SystemClock.elapsedRealtime();

// Tell the user about what we did.
Toast.makeText(AndroidAlarmService.this, "Resume!",
Toast.LENGTH_LONG).show();

}
});

我想从启动点恢复应用程序。我该怎么做?

谢谢你!

最佳答案

关于android - 如何暂停和恢复 Android 闹钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13926092/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com