Search code examples
androidandroid-intentalarmmanagerandroid-pendingintent

Pending Intent and Alarm Manager


I have been making an Android app lately.. In it I have used Pending Intent, with Alarm Manager. I need to have multiple pending intents, and so I am using FLAG_ONE_SHOT. Alarm Manager will send broadcast at mentioned interval. And also, along with that I am using intent's setAction() method and passing currentTimeMillis() as Argument. And I have corresponding Broadcast Receiver. The problem is that once the app is closed, or deleted from the recents tray, the Broadcast receiver is not running. The code is as follow:

  1. setAlarm:

    private void setupAlarm(int seconds) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(getBaseContext(), OnAlarmReceive.class);
    //PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    intent.setAction(Long.toString(System.currentTimeMillis()));
    intent.putExtra("id", ID);
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ChatActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    
    Log.e(TAG, "Setup the Alarm");
    
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.SECOND, seconds);
    
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);}
    
  2. Broadcast Receiver

    public void onReceive(Context context, Intent intent) {
    
    String id = intent.getStringExtra("id");
    Log.e(TAG,"On the verge of deleting the message with id: "+id);
    SQLiteDatabase database = context.openOrCreateDatabase("/sdcard/userlists.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
    database.execSQL("DELETE FROM " + "MESSAGE" + " WHERE " + "id" + "= '" + id + "'");
    
    broadcaster = LocalBroadcastManager.getInstance(context);
    intent = new Intent(COPA_RESULT);
    broadcaster.sendBroadcast(intent);}
    
  3. Manifest.xml

    <receiver android:name=".OnAlarmReceive" android:enabled="true" android:exported="true"/>
    

Please help me. I need the Broadcaster to do the job, even if the app is closed.


Solution

  • It's process life-cycle bug in which system may kill process when app goes in background to reclaim memory

    You need to schedule JobService for receiving job whether application is active or not

    from official document of Processes and Application Life Cycle

    A common example of a process life-cycle bug is a BroadcastReceiver that starts a thread when it receives an Intent in its BroadcastReceiver.onReceive() method, and then returns from the function. Once it returns, the system considers the BroadcastReceiver to be no longer active, and thus, its hosting process no longer needed (unless other application components are active in it). So, the system may kill the process at any time to reclaim memory, and in doing so, it terminates the spawned thread running in the process. The solution to this problem is typically to schedule a JobService from the BroadcastReceiver, so the system knows that there is still active work being done in the process.

    Here is example you can follow to complete your requirement