我有一個簡單的廣播接收機,監聽連接的變化,如果網絡可用,它會設置一個重複發生的警報,開始一個短暫的服務。如果沒有網絡連接時,它會禁用重複報警:連接變更接收器在啓動後是否存活/啓動?
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 :(");
}
}
}
清單看起來像:
<receiver android:name="ConnectivityChange">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
所有這一切都工作得很好。
然而,即使我重新啓動模擬器後,此代碼似乎仍然運行。我沒有要求BOOT_COMPLETE。我確信我沒有從快照中恢復模擬器。這是預期的行爲?我不確定這是記錄在哪裏。當我遇到這個問題時,我正要求BOOT_COMPLETE。
但我*不*要求BOOT_COMPLETED。我只註冊了CONNECTIVITY_CHANGE,但我的應用似乎在啓動時運行。 –