0

I have a simple broadcast receiver that listens in for connectivity changes and if a network is available, it would set a recurring alarm that would start a short-lived service. If there is no network connection, it would disable the recurring alarm:

public class ConnectivityChange extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(getClass().getName(), "Connectivity changed! :/");

        MyApplication app = (MyApplication ) context
                .getApplicationContext();


        if (app.isConnected()) {
            // setup repeating alarms, since we are connected.
            app.setCurrencyRatesServiceRepeatingAlarm(context);
            Log.d(getClass().getName(), "Connected :D");
        } else {
            // cancel any repeating alarm, since we are NOT connected.
            app.unsetCurrencyRatesServiceRepeatingAlarm(context);
            Log.d(getClass().getName(), "Not connected :(");
        }


    }

}

The manifest looks like:

<receiver android:name="ConnectivityChange">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
    </intent-filter> 
</receiver>

All of that works just fine.

HOWEVER, this code seem to run even after I reboot the emulator. I'm not asking for BOOT_COMPLETE. I made sure I didn't restore the emulator from a snapshot. Is this expected behavior? I'm not sure where this is documented. I was about to ask for BOOT_COMPLETE when I came across this.

Charles
  • 50,943
  • 13
  • 104
  • 142
Jay Sidri
  • 6,271
  • 3
  • 43
  • 62

2 Answers2

1

I know this is a somewhat old question, but I thought I'd weigh in anyway. It's quite possible that your connectivity changes whenever you boot, for example that the device goes from no connection to a connection. Therefore your code will run everytime the device boots.

soren.qvist
  • 7,376
  • 14
  • 62
  • 91
0

You can register a BOOT_COMPLETED broadcast receiver, which will get control even if RECEIVE_BOOT_COMPLETED is not requested as a permission.

The Bug is already reported here

even you can check this SO thread

Community
  • 1
  • 1
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • But I'm *not* asking for BOOT_COMPLETED. I only registered for CONNECTIVITY_CHANGE and yet my app seem to run on startup. – Jay Sidri Jun 05 '12 at 09:43