java 中的网络状态投射问题

Network state casting issue in java

我一直在应用程序内使用网络检查功能,但现在所有东西都可以在 Kotlin 中使用同样的东西想要集成到 java 面临一些与延迟调用相关的问题。

这是我在 Kotlin 中检查网络状态的方法

class MainActivity : AppCompatActivity(), ConnectivityStateListener {

    private lateinit var tv: TextView
    private val provider: ConnectivityProvider by lazy { ConnectivityProvider.createProvider(this) }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        tv = findViewById(R.id.connectivity_state)
        val button = findViewById<View>(R.id.button)
        val currentState = findViewById<TextView>(R.id.current_state)

        button.setOnClickListener {
            val hasInternet = provider.getNetworkState().hasInternet()
            currentState.text = "Connectivity (synchronously): $hasInternet"
        }
    }

    override fun onStart() {
        super.onStart()
        provider.addListener(this)
    }

    override fun onStop() {
        super.onStop()
        provider.removeListener(this)
    }

    override fun onStateChange(state: NetworkState) {
        val hasInternet = state.hasInternet()
        tv.text = "Connectivity (via callback): $hasInternet"
    }

    private fun NetworkState.hasInternet(): Boolean {
        return (this as? ConnectedState)?.hasInternet == true
  



 }
}

我就是这样融入Java

@Override
    protected void onStart() {
        super.onStart();
        provider=ConnectivityProvider.createProvider(this);
        provider.addListener(this);
    }


@Override
protected void onStop() {
    super.onStop();

    provider.removeListener(this);
}

  @Override
    public void onStateChange(@NotNull ConnectivityProvider.NetworkState state) {

        Log.d("To ConnectivityProvider-----", state.toString());
        Toast.makeText(LoginActivity.this, "Available", Toast.LENGTH_SHORT).show();
       if( hasInternet(state)){
           Toast.makeText(LoginActivity.this, "Available", Toast.LENGTH_SHORT).show();
       }else{
            Toast.makeText(LoginActivity.this, "No Internet", Toast.LENGTH_SHORT).show();

       }

    }

    private boolean hasInternet(@NotNull ConnectivityProvider.NetworkState state) {

        ConnectivityProvider.NetworkState.ConnectedState var2 = (ConnectivityProvider.NetworkState.ConnectedState)state;
        if (var2.getHasInternet()) {

            return true;
        }
        return false;
    }

java.lang.ClassCastException: com.ro.other.connectivity.base.ConnectivityProvider$NetworkState$NotConnectedState cannot be cast to com.ro.other.connectivity.base.ConnectivityProvider$NetworkState$ConnectedState

gitlink 帮我把它整合到 java

这里的问题是类型转换,而不是惰性调用。给定 Kotlin 和 Java 代码中 hasInternet 方法的行为不同。

科特林:

return (this as? ConnectedState)?.hasInternet == true

如果当前 NetworkState 不是 ConnectedState,类型转换 this as? ConnectedState 将为 return null,并且该方法将为 return false。

Java:

ConnectivityProvider.NetworkState.ConnectedState var2 = (ConnectivityProvider.NetworkState.ConnectedState)state;
if (var2.getHasInternet()) {
    return true;
}
return false;

此代码假设 state 是一个ConnectedState(而不是检查),并抛出ClassCastException 如果它是另一个 class.

的实例

hasInternet 方法的正确 Java 代码为:

if (!(state instanceof ConnectivityProvider.NetworkState.ConnectedState)) {
    return false;
}
ConnectivityProvider.NetworkState.ConnectedState var2 = (ConnectivityProvider.NetworkState.ConnectedState)state;
if (var2.getHasInternet()) {
    return true;
}
return false;