Ali Faris
Ali Faris

Reputation: 18592

AlarmManager not working when app is closed

I want to to run some task every X minute. I'm using AlarmManager and it is work fine when app is running or in background. However, it stop working after I remove the app from running apps list.

Alarm Setting:

Intent intent = new Intent(context , SoundNotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0 , intent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE , 15);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis() , pendingIntent);
}
else {
    alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis() , pendingIntent);
}

System.out.println("Alarm is set : " + c.getTime());

manifest.xml:

<receiver android:name="info_tech.com.fazeker.SoundNotificationReceiver"
    android:exported="true"
    android:enabled="true">
</receiver>

Upvotes: 0

Views: 1527

Answers (2)

Jo&#227;o Vitor
Jo&#227;o Vitor

Reputation: 11

I know that it is late but i resolved the same problem calleding the alarm in onDestroy:

OnDestroy in BaseActivity:

@Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG,getClasName()+ ".onDestroy Calleding: ");
        AlarmManager_Custom alarm = new AlarmManager_Custom(new Intent(this,BC_Custom.class),this);
        }

My AlarmManager in kotlin:

    class AlarmManager_Custom(val intent: Intent, val context: Context) {
    val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)
    val alarm = context.getSystemService(ALARM_SERVICE) as AlarmManager
    private var calendar = Calendar.getInstance()

    fun setTimeCalendar() {
        calendar.timeInMillis = System.currentTimeMillis()
        calendar.add(Calendar.MINUTE, 1)

        if (Build.VERSION.SDK_INT < 23) {
            if (Build.VERSION.SDK_INT >= 19) {
                alarm.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
            } else {
                alarm.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
            }
        } else {
            alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
        }
    }
}

My BroadCastReceiver:

    public class BC_Custom extends BroadcastReceiver {
    @SuppressLint("UnsafeProtectedBroadcastReceiver")
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("TAG_RECEIVER", "broad cast called");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(new Intent(context, UpdateDados.class));
        } else {
            context.startService(new Intent(context,UpdateDados.class));
        }
    }
}

Upvotes: 1

Gil Snovsky
Gil Snovsky

Reputation: 210

use service like :

public class TestJobService extends JobService {
  private static final String TAG = "SyncService";


  @Override
  public boolean onStartJob(JobParameters params) {
    // We don't do any real 'work' in this sample app. All we'll
    // do is track which jobs have landed on our service, and
    // update the UI accordingly.
    Log.i(TAG, "on start job: " + params.getJobId());
    return true;
  }

  @Override
  public boolean onStopJob(JobParameters params) {
    Log.i(TAG, "on stop job: " + params.getJobId());
    return true;
  }

  MainActivity mActivity;
  private final LinkedList<JobParameters> jobParamsMap = new LinkedList<JobParameters>();

  public void setUiCallback(MainActivity activity) {
    mActivity = activity;
  }

} 

Upvotes: 0

Related Questions