I m novice in Android . Please help me to solve my problem ! my app is working online and offline ! when Wifi is connected but there is no internet connection app must be switched to offline mode. and for offline mode i was using this code:
public class Connectivity {
public static boolean isOnline() {
try {
Process p1 = Runtime.getRuntime().exec("ping -c 1 -W1 www.google.com");
int returnVal = p1.waitFor();
boolean reachable = (returnVal == 0);
return reachable;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_online_chat,container,false);
WebView webView =(WebView)v.findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("url");
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.layout);
RelativeLayout relativeLayout =(RelativeLayout)v.findViewById(R.id.llout);
Button btnConnect =(Button)v.findViewById(R.id.btnRetry);
if (Connectivity.isOnline()){
relativeLayout.setVisibility(View.VISIBLE);
linearLayout.setVisibility(View.INVISIBLE);
}
else {
relativeLayout.setVisibility(View.INVISIBLE);
linearLayout.setVisibility(View.VISIBLE);
}
return v;
}
But its not working with some phone models. I have been suffering with this problem for 2 weeks already. I've tried everything on this website (all methods) but it doesn't work for me . What i need to do ???