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.connection.base.ConnectivityProvider$NetworkState$NotConnectedState无法强制转换为com.ro.other.connection.base.ConnectivityProvider$NetworkState$ConnectedState

gitlink帮助我将其集成到java中

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

Kotlin:

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

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

Java:

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

此代码假设stateConnectedState(而不是检查(,如果它是不同类的实例,则抛出ClassCastException

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;

最新更新