Dwarakesh Pallagolla
Dwarakesh Pallagolla

Reputation: 483

Android Oreo: Broadcast receiver does not work when application is not in memory

I have a requirement where I need to show a notification to the user at specific times during a week.

I tried using the alarm manager to achieve this.

The issue I am facing is that the onRecieve() method of my broadcast listener works fine when the application is in memory, but if I kill the application (Note: not force stop, I only remove the application from the memory), the onRecieve method is not called.

I can see from the logcat that the alarmManager is sending the correct intent, but my application does not seem to start for some reason.

I have combed through every possible resource regarding this and none of them seem to have solved my issue.

My Manifest:

Permissions:

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

Intent receiver in the manifest:

     <receiver android:name=".Alarm">
             <intent-filter>
                 <action android:name="android.intent.action.NOTIFY" />
                 <action android:name="com.myApp.myCustomThing" />
             </intent-filter>
     </receiver>

My Broadcast receiver:

public class Alarm extends BroadcastReceiver
{
    Context context;
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.i("Info","Inside OnReceive");
        this.context = context;
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        Log.i("Info","Alarm called - showing toast");
        // Put here YOUR code.
        Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

        Log.i("Info","Trying to create notification");

        wl.release();
    }

    public void setAlarm(Context context)
    {
        Log.i("Info","In Set Alarm");
        AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, Alarm.class);
        i.setAction("com.myApp.myCustomThing");
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
//        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute
        am.set(AlarmManager.RTC_WAKEUP,1000*60,pi);
        Log.i("Info","Alarm Set");
    }

}

I see the below entry in the logcat, the log message appears irrespective of the application being in memory or not.

 01-15 14:08:06.967 1396-1673/? V/AlarmManager: Triggering alarm #0: 2
 when =60399531 package =notification.me.com.myApp operation
 =*walarm*:com.myApp.myCustomThing flags =0x5

Upvotes: 1

Views: 848

Answers (1)

azizbekian
azizbekian

Reputation: 62209

You have explicitly disabled your broadcast receiver in manifest file and I cannot see where from you enable it, which means that it will never receive any broadcast.

Remove android:enabled="false" from manifest file.

Upvotes: 1

Related Questions