如何暂停和恢复Android闹钟?

时间:2012-12-18 04:08:16

标签: android alarmmanager android-alarms

我正在开发一种警报,它会按照规定的时间间隔振动。因此,如果我选择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();

        }
    });

我想从它开始的那一点开始恢复应用程序。 我怎么能这样做?

谢谢,你!

1 个答案:

答案 0 :(得分:0)