没有网络时打开另一个活动[重复]

Posted

技术标签:

【中文标题】没有网络时打开另一个活动[重复]【英文标题】:Open Another Activity When There Is No Network [duplicate] 【发布时间】:2019-02-19 06:41:56 【问题描述】:

如果没有网络,我想打开另一个显示没有互联网连接的活动。我可以这样做吗?请帮忙。

【问题讨论】:

只需将here 的代码添加到您的班级并与if(isConnected()) startActivity(...) 核对或不要开始或其他... 【参考方案1】:
inline fun Context.withNetwork(isToNotify: Boolean = true, block: () -> Unit) 
    val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
    connectivityManager?.let 
        val netInfo = it.activeNetworkInfo
        val isConnected = netInfo != null && netInfo.isConnectedOrConnecting
        if (isToNotify && !isConnected)
            toast(R.string.message_no_network_message)
             //Use Here Intent For Another Activity

        else
            block()
        
    

【讨论】:

以上是关于没有网络时打开另一个活动[重复]的主要内容,如果未能解决你的问题,请参考以下文章