-1

I have an app where I want to set alarm on specific date and time . Let say 5:00pm of 16 march. I want this alarm to be schedule as non repeating alarm.

Now the alarm is setting and working. but it is only working when App is working or in background. but when app is force closed it is not working.

here is what I am doing:

 public void startAlert() {  
        EditText text = (EditText) findViewById(R.id.time);  
        int i = Integer.parseInt(text.getText().toString());  
        Intent intent = new Intent(this, MyBroadcastReceiver.class);  
        PendingIntent pendingIntent = PendingIntent.getBroadcast(  
                                      this.getApplicationContext(), 234324243, intent, 0);  
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()  
                                      + (i * 1000), pendingIntent);  
        Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();  
    } 

Well this code is just for demonstration. The problem is when I have cleared my app from the app task manager the alaram does not alaram.

I know there are some problems, as android os unregister scheduled alarms , also there could be problem on reboot.

For the time Now I really do not care about about the reboot, but alarm should work when app is forced to close. If it is not applicable in android then how other apps are doing this ???

Please share your views about it ? and tell me anyother solution to how to notify my user at specific time of a day even my app is not there even in background ?? please help

Vinayak B
  • 4,430
  • 4
  • 28
  • 58
Android teem
  • 780
  • 2
  • 13
  • 36

2 Answers2

0

for setting Alarm .please use below code

if (SDK_INT < Build.VERSION_CODES.KITKAT) {
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()  
                                  + (i * 1000), pendingIntent);
}
else if (Build.VERSION_CODES.KITKAT <= SDK_INT  && SDK_INT < Build.VERSION_CODES.M) {
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()  
                                  + (i * 1000), pendingIntent);
}
else if (SDK_INT >= Build.VERSION_CODES.M) {
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()  
                                  + (i * 1000), pendingIntent);
}
Vinayak B
  • 4,430
  • 4
  • 28
  • 58
0

From AlarmManager

AlarmManager provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted

Qandil Tariq
  • 539
  • 1
  • 3
  • 15