如何在PendingIntent中使用registerNetworkCallback? [英] How to use registerNetworkCallback with PendingIntent?

查看:195
本文介绍了如何在PendingIntent中使用registerNetworkCallback?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CONNECTIVITY_ACTION .使用registerNetworkCallback(NetworkRequest,PendingIntent).

CONNECTIVITY_ACTION was deprecated in API level 28 and Google advices to use registerNetworkCallback(NetworkRequest, PendingIntent).

我尝试使用 ConnectivityManager.NetworkCallback ,它可以工作,但是我想使用PendingIntent.

I tried registerNetworkCallback with ConnectivityManager.NetworkCallback and it works, but I want to use PendingIntent.

公共无效registerNetworkCallback(NetworkRequest请求,PendingIntent操作):

该操作是转到广播接收器的Intent广播 您通过Context#registerReceiver或通过 标记在AndroidManifest.xml文件中

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

Context#registerReceiver接受IntentFilter作为参数,而PendingIntent.getBroadcast要求Intent.

Context#registerReceiver accepts IntentFilter as an argument, while PendingIntent.getBroadcast requires Intent.

这是我的代码,没有调用NetworkStateReceiver :: onReceive.

Here is my code, and NetworkStateReceiver::onReceive is not called.

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())
}

为什么? 我还应该打电话给Context#registerReceiver吗?但是,如果不推荐使用CONNECTIVITY_ACTION,我应该对IntentFilter采取什么措施?

Why? Should I also call Context#registerReceiver? But what action should I use with IntentFilter if CONNECTIVITY_ACTION was deprecated?

推荐答案

最后,我成功了.首先,我需要将接收器添加到AndroidManifest.xml

Finally I made it works. First I need to add the receiver to AndroidManifest.xml

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

第二,NetworkStateReceiver#onReceive仅在连接脱机并可用时被调用一次.因此,每当连接脱机时我需要发送任何内容,我都必须致电registerNetworkUpdates.

Second, NetworkStateReceiver#onReceive is called only once when the connection was offline and becomes available. So every-time I need to send anything when the connection is offline I have to call registerNetworkUpdates.

第三,在致电registerNetworkCallback之前,我必须先致电unregisterNetworkCallback.如果我用相同的Intent两次调用了registerNetworkCallback,则NetworkStateReceiver#onReceive被调用了两次.在创建PendingIntent.getBroadcast时,我使用的是applicationContext而不是this.

Third, I have to call unregisterNetworkCallback before calling registerNetworkCallback. If I called registerNetworkCallback twice with the same Intent, NetworkStateReceiver#onReceive was called twice. And I used applicationContext instead of this when creating PendingIntent.getBroadcast.

这篇关于如何在PendingIntent中使用registerNetworkCallback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆