5

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

        }
    });

我想从应用程序的启动点恢复应用程序。我怎样才能做到这一点?

谢谢!

4

1 回答 1

-1

以下链接可能对您有帮助

http://android-er.blogspot.in/2010/10/simple-example-of-alarm-service-using.html

于 2012-12-18T04:30:40.947 回答