【问题标题】:Connectivity Change Receiver survives/starts after boot?连接更改接收器在启动后仍然存在/启动?
【发布时间】:2012-11-19 22:26:34
【问题描述】:

我有一个简单的广播接收器,它监听连接变化,如果网络可用,它会设置一个循环警报,启动一个短期服务。如果没有网络连接,它将禁用重复警报:

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。

【问题讨论】:

    标签: android connectivity bootcompleted


    【解决方案1】:

    我知道这是一个有点老的问题,但我想我还是会权衡一下。每当您启动时,您的连接很可能会发生变化,例如设备从无连接变为连接。因此,您的代码将在每次设备启动时运行。

    【讨论】:

      【解决方案2】:

      您可以注册一个 BOOT_COMPLETED 广播接收器,即使没有请求 RECEIVE_BOOT_COMPLETED 作为权限,它也会获得控制权。

      该错误已报告here

      你也可以查看this SO线程

      【讨论】:

      • 但我要求 BOOT_COMPLETED。我只注册了 CONNECTIVITY_CHANGE,但我的应用似乎在启动时运行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 2023-03-23
      相关资源
      最近更新 更多