I have been using a network check functionality in-app, but all things are available in Kotlin now the same thing wants to integrate in java facing some issue related to lazy calling.
This is how I can check network state in 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
}
}
this is how I integrated into 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 help me to integrate this in java
CodePudding user response:
The problem here is with type casting, not with lazy calling. The behaviour of the hasInternet
methods in the given Kotlin and Java code is different.
Kotlin:
return (this as? ConnectedState)?.hasInternet == true
If the current NetworkState
is not a ConnectedState
, the typecast this as? ConnectedState
will return null, and the method will return false.
Java:
ConnectivityProvider.NetworkState.ConnectedState var2 = (ConnectivityProvider.NetworkState.ConnectedState)state;
if (var2.getHasInternet()) {
return true;
}
return false;
This code assumes that state
is a ConnectedState
(instead of checking it), and throws a ClassCastException
if it is an instance of a different class.
The correct Java code for hasInternet
method would be:
if (!(state instanceof ConnectivityProvider.NetworkState.ConnectedState)) {
return false;
}
ConnectivityProvider.NetworkState.ConnectedState var2 = (ConnectivityProvider.NetworkState.ConnectedState)state;
if (var2.getHasInternet()) {
return true;
}
return false;