4
我正在開發一個鬧鐘,它會在指定的時間間隔內振動。所以如果我選擇5秒鐘,應用程序會每隔5秒鐘振動一次。但我需要停下來,暫停並恢復應用程序。如何暫停和恢復Android鬧鐘?
這是我的課:
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();
}
});
我想恢復應用程序從它開始的點。 我該怎麼做?
謝謝,你!
謝謝下面的鏈接可能會有所幫助。但這正是我用來做我的例子的鏈接。 – androidevil
嘗試重置鬧鐘,而不是恢復 –