Search code examples
androidandroid-serviceandroid-broadcastreceiver

BroadcastReceiver dies with app


If i let the phone sit for a long time like 15 minutes i lose my receiver but i thought it was to persist like a service after being killed for memory.

Manifest:

<receiver
    android:name=".WearableReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="com.example.johnbravado.MESSAGE_PROCESSED"/>
    </intent-filter>
</receiver>

In Activity to start receiver

ComponentName component = new ComponentName(CounterActivity.this, WearableReceiver.class);
getPackageManager()
    .setComponentEnabledSetting(component,
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
        PackageManager.DONT_KILL_APP);

The receiver

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    //MyConstants.getInstance().showToast("Message Rcvd");
    PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "com.example.johnbravado");
    wakeLock.acquire();
    // Do Work
    MyConstants.getInstance().msgReqAction(intent.getIntExtra(MyConstants.BROADCAST_DATA_REQ, 0));

    wakeLock.release();
}

The broadcast sender

String BROADCAST_ACTION_RESP = "com.example.johnbravado.MESSAGE_PROCESSED"
@Override
public void onMessageReceived(final MessageEvent messageEvent) {
    nodeId = messageEvent.getSourceNodeId();
    String incomingPath = messageEvent.getPath();
    int incomingReq = Integer.parseInt(new String(messageEvent.getData()));

    if(incomingPath.equalsIgnoreCase(MyConstants.MSG_COUNTER_REQ_PATH)) {
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(BROADCAST_ACTION_RESP);
        broadcastIntent.putExtra(MyConstants.BROADCAST_DATA_REQ, incomingReq);
        sendBroadcast(broadcastIntent);

    }else if(incomingPath.equalsIgnoreCase(MyConstants.MSG_DEFAULT_PATH)){

    }
}

only way I get this to persist for long periods of time is to invoke a service

wearableReceiverIntent = new Intent(this, WearableReceiverService.class);
if(!WearableReceiverService.isRunning())
    startService(wearableReceiverIntent);

the service

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.
    IntentFilter filter = new IntentFilter(MyConstants.BROADCAST_ACTION_RESP);
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    receiver = new WearableReceiver();
    registerReceiver(receiver, filter);

Notification notification = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_notif_bible)
        .setContentText("Preaching").build();
startForeground(MyConstants.NOTIF_COUNTING_SERVICE, notification);

    isRunning = true;
    return START_STICKY;
}

If I run the service it persists for long periods of time but it drains the battery unnecessarily considering I interact only once every 10 minutes. I was under impression Broadcast receiver would work like service except for short bursts of work. invoke the service if you need to do long actions.


Solution

  • Greenify was killing my app when the screen went off. I was battling something I had no hope of defending against with code. After I explicitly told Greenify to not kill my app, I never told it to kill my app to begin with, everything worked as intended.