Android AlarmManager setRepeating中的问题

时间:2013-12-20 09:41:25

标签: android alarmmanager

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DataBaseHelper db = new DataBaseHelper(getApplicationContext());
        labels = db.getAllLabels();


        if (labels.isEmpty()) {
            setContentView(R.layout.activity_main);
            System.out.println("in if loop");
        } else {
            System.out.println("in else loop");

            Intent i = new Intent(this, activity2.class);
            startActivity(i);
            MainActivity.this.finish();
            return;
        }

        System.out.println("Coming into onCreate");


        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());

        cal.add(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 10);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 10);
        System.out.println(cal.toString() + "         " + cal.getTime());
    //  if(cal.DAY_OF_WEEK == C)

        Intent intent = new Intent(MainActivity.this, TestService.class);

        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        // int i;
        // i=24*60*60*1000;
        alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis() ,
                AlarmManager.INTERVAL_DAY, pintent);

//      head = (TextView) findViewById(R.id.textView1);
//      tname = (TextView) findViewById(R.id.textView2);
//      tid = (TextView) findViewById(R.id.textView3);
//      tphone = (TextView) findViewById(R.id.textView4);
        ename = (EditText) findViewById(R.id.reg_fullname);
        eid = (EditText) findViewById(R.id.reg_email);
        ephone = (EditText) findViewById(R.id.reg_password);
        submit = (Button) findViewById(R.id.btnRegister);

        // head.setText("REGISTER");
        // tname.setText("NAME:");
        // tid.setText("ID:");
        // tphone.setText("PHONE NUMBER");

        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                name = ename.getText().toString();
                id = eid.getText().toString();
                phone = ephone.getText().toString();

                label.add(name);
                label.add(id);
                label.add(phone);

                DataBaseHelper db = new DataBaseHelper(getApplicationContext());
                // labels=db.getAllLabels();
                db.insertLabel(label);
                Toast.makeText(getApplicationContext(), "Registered",
                        Toast.LENGTH_SHORT).show();
                MainActivity.this.finish();
            }
        });

    }

在此,我已将警报管理器设置为在早上10点触发,并且必须每天重复。但是在触发警报之后会以1小时,38分钟等不同间隔重复发出警报。我也尝试过将intervalMills设为86400000,但重复时间仍然出错。请帮帮我。提前致谢

1 个答案:

答案 0 :(得分:1)

alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pintent);

将以上代码替换为以下代码:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),1L * 24L * 60L * 60L * 1000L,pendingIntent);

我希望它有所帮助