Rajkumar
Rajkumar

Reputation: 189

Issue in Android AlarmManager setRepeating

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();
            }
        });

    }

In this I have set the alarm manager to trigger at 10 am in the morning and that has to repeat everyday. But after triggering the alarm gets repeated at various interval say 1 hour, 38 mins and so on. I have also tried by giving intervalMills as 86400000 but still the repeating time goes wrong. Please do help me. Thanks in advance

Upvotes: 1

Views: 674

Answers (1)

vadher jitendra
vadher jitendra

Reputation: 590

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

Replace above code with this one:

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

i hope it help

Upvotes: 1

Related Questions