0

谁能帮我解决我的问题,我必须让闹钟在当前时间 10 分钟后响起,并且必须每 10 分钟重复一次,我写了这段代码但它没有用,它随机开始:”

public class AlarmNewActivity extends Activity {
    /** Called when the activity is first created. */

    Intent intent;
    PendingIntent sender;
     AlarmManager am;
     Button bStart, bStop ;
     long mCurrentTime, firstTime ;
     Calendar calendar;
     TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        bStart = (Button) findViewById (R.id.button1);
        bStop = (Button) findViewById (R.id.button2);
        tv = (TextView) findViewById (R.id.textView1);

        intent = new Intent(AlarmNewActivity.this, RepeatingAlarm.class);
        sender = PendingIntent.getBroadcast(AlarmNewActivity.this, 0, intent, 0);
        am = (AlarmManager)getSystemService(ALARM_SERVICE);
        calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());


        mCurrentTime = calendar.getTimeInMillis();


        bStart.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                tv.setText(""+ firstTime + "\n" + mCurrentTime );

                am.setRepeating(AlarmManager.RTC_WAKEUP,
                        mCurrentTime + 10 *1000, 5*1000, sender);
               new Handler().postDelayed(new Runnable() {
                    public void run() {
                         bStop.performClick();
                    }
                }, ( mCurrentTime + 50 * 1000 )); 

            }
        });


        //==================================================================================

        bStop.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                am.cancel(sender);
            }
        });
     }
}
4

1 回答 1

3

mCurrentTime = calendar.getTimeInMillis();

应该在 onClick 里面获取点击时的当前时间

public void onClick(View v) {

mCurrentTime = calendar.getTimeInMillis();
...
...
}
于 2012-05-07T15:30:49.000 回答