开发者

Why isn't my AlarmManager setRepeating repeating?

I've created an AlarmManager that should run repeatedly and start a service. My problem is it only runs once. I can't figure out what is wrong. For now I've attached creation of the AlarmManager to a button.

This is part of the MenuActivity class:

private void startUpdateService() {
    Log.i(TAG, "Registering AlarmManager");

    Intent service = new Intent(MenuActivity.this, UpdateService.class);
    pendingIntent = PendingIntent.getService(MenuActivity.this, 0, service, 0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, 10*1000, pendingIntent);

}

@Override
public void onClick(View view) {
    switch(view.getId()) {
    case R.id.button_calendar:
        Intent calendarIntent = new 开发者_JS百科Intent(this, CalendarActivity.class);
        if(Util.isOnline(this)){
            startActivity(calendarIntent);
        }else {
            Toast.makeText(MenuActivity.this, R.string.toast_is_online, Toast.LENGTH_LONG).show();
        }
        break;
    case R.id.button_settings:
        startUpdateService();           
        Toast.makeText(MenuActivity.this, "Startet alarm", Toast.LENGTH_LONG).show();           
        break;
    }

}

Initial alarm is working fine, but it won't repeat. Anyone that knows what I'm doing wrong?


I have seen few problems with running service straight from the alarm. I had it too. What i did, was replacing the service by the broadcast reciver which fires up the service. Why won't you try it too? It's explained well in Advanced Android, ver 1 is available on CC licence.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜