报警android应用程序

时间:2012-05-07 15:24:32

标签: java android alarm

任何人都可以帮我解决我的问题, 我必须在当前时间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);
            }
        });
     }
}

1 个答案:

答案 0 :(得分:3)

mCurrentTime = calendar.getTimeInMillis();

应该在onClick内部获取点击时的当前时间

public void onClick(View v) {

mCurrentTime = calendar.getTimeInMillis();
...
...
}