简体   繁体   English

如何将 registerNetworkCallback 与 PendingIntent 一起使用?

[英]How to use registerNetworkCallback with PendingIntent?

CONNECTIVITY_ACTION was deprecated in API level 28 and Google advices to use registerNetworkCallback(NetworkRequest, PendingIntent). CONNECTIVITY_ACTION在 API 级别 28 中已弃用,Google 建议使用 registerNetworkCallback(NetworkRequest, PendingIntent)。

I tried registerNetworkCallback with ConnectivityManager.NetworkCallback and it works, but I want to use PendingIntent.我尝试使用ConnectivityManager.NetworkCallback进行 registerNetworkCallback 并且它有效,但我想使用 PendingIntent。

What confuse me is the description of public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) :让我困惑的是public void registerNetworkCallback(NetworkRequest request, PendingIntent operation)的描述:

The operation is an Intent broadcast that goes to a broadcast receiver that you registered with Context#registerReceiver or through the tag in an AndroidManifest.xml file该操作是一个 Intent 广播,它发送到您使用 Context#registerReceiver 或通过 AndroidManifest.xml 文件中的标记注册的广播接收器

Context#registerReceiver accepts IntentFilter as an argument, while PendingIntent.getBroadcast requires Intent . Context#registerReceiver接受IntentFilter作为参数,而PendingIntent.getBroadcast需要Intent

Here is my code, and NetworkStateReceiver::onReceive is not called.这是我的代码,没有调用 NetworkStateReceiver::onReceive。

private fun getNetworkIntent(): PendingIntent {
    if (networkPendingIntent != null) {
        return networkPendingIntent!!
    }

    val intent = Intent(this, NetworkStateReceiver::class.java)
    networkPendingIntent = PendingIntent.getBroadcast(
        this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

    return networkPendingIntent!!
}

private fun getNetworkRequest(): NetworkRequest {
    return NetworkRequest.Builder()
        .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
        .build()
}

private fun registerNetworkUpdates() {
    var cm =
        applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
    cm?.registerNetworkCallback(getNetworkRequest(), getNetworkIntent())
}

Why?为什么? Should I also call Context#registerReceiver ?我还应该调用Context#registerReceiver吗? But what action should I use with IntentFilter if CONNECTIVITY_ACTION was deprecated?但是,如果不推荐使用CONNECTIVITY_ACTION ,我应该对IntentFilter使用什么操作?

Finally I made it works.最后我让它工作了。 First I need to add the receiver to AndroidManifest.xml首先我需要将接收器添加到 AndroidManifest.xml

<receiver 
        android:name=".NetworkStateReceiver"
        android:enabled="true"
        android:exported="true" />

Second, NetworkStateReceiver#onReceive is called only once when the connection was offline and becomes available.其次, NetworkStateReceiver#onReceive仅在连接脱机并可用时调用一次。 So every-time I need to send anything when the connection is offline I have to call registerNetworkUpdates .所以每次我需要在连接离线时发送任何东西时,我都必须调用registerNetworkUpdates

Third, I have to call unregisterNetworkCallback before calling registerNetworkCallback .第三,我必须在调用registerNetworkCallback之前调用unregisterNetworkCallback If I called registerNetworkCallback twice with the same Intent , NetworkStateReceiver#onReceive was called twice.如果我用相同的Intent调用了registerNetworkCallback两次,则NetworkStateReceiver#onReceive被调用了两次。 And I used applicationContext instead of this when creating PendingIntent.getBroadcast .我在创建PendingIntent.getBroadcast时使用了applicationContext而不是this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM