2

My app use AlarmManager. I try next code for create Alarm

public void startAlarm(int timeInterval){
    if (timeInterval == -1)
        return;
    int id = generateIdAlarmManager();
    AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = getPendingIntent(id, timeInterval * 60000);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        final AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(System.currentTimeMillis() + timeInterval * 60000, pendingIntent);

        manager.setAlarmClock(alarmClockInfo, pendingIntent);
    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        manager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInterval * 60000, pendingIntent);
    else
        manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInterval * 60000, pendingIntent);

    SharedPreferences.Editor edit = preferences.edit();
    edit.putInt(ALARM_MANAGER_ID_ALARM_CHECK_SMS, id);
    edit.putInt(ALARM_MANAGER_INTERVAL_CHECK_SMS, timeInterval);
    edit.putBoolean(ALARM_MANAGER_ENABLE_CHECK_SMS, true);
    edit.apply();
    edit.commit();
}

AlarmManager working if screen on. If screen off AlarmManager not working. I add

<uses-permission android:name="android.permission.WAKE_LOCK"/>

Help!

user1854307
  • 580
  • 4
  • 8
  • 21

2 Answers2

1

Your question is answered here using the WakeLock object.

More information about WaveLock in Android Developers:

A wake lock is a mechanism to indicate that your application needs to have the device stay on.

Community
  • 1
  • 1
Effie
  • 11
  • 4
1

call

manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),timeIntervalForBroadcast, pendingIntent)

instead of manager.set() method of alaramManager.

Muhib Pirani
  • 765
  • 1
  • 6
  • 15
  • This solution is not working. I try such code manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), timeInterval * 60000, pendingIntent); – user1854307 Feb 18 '17 at 16:14