I have an app and in that, I want to check the real-time network of the mobile phone.
This is my java code:
This is only some of the full java file.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
if (checkNetwork() == false) {
Toast noNetwork = Toast.makeText(getApplicationContext(), "Sync is paused, due to no
internet...", Toast.LENGTH_LONG);
noNetwork.show();
} else if (checkNetwork() == true) {
Toast yesNetwork = Toast.makeText(getApplicationContext(), "Sync is turned on...",
Toast.LENGTH_LONG);
yesNetwork.show();
}
// This code we will add later - bottomNavigation.getMenu().removeItem(R.id.navigation_notes);
}
private boolean checkNetwork() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
If I open my app and navigate to this page, it shows the internet is there
toast, but at the same time when I turn off mobile data, the other toast which shows internet is not there
is not shown. I think the above code is not real-time
or there is a problem with the toast, I don't know.
I want to know if there are ways in which I could make the above code real-time. Thanks in advance.