In my OnCreate
Method I am checking my Internet connection. If there is no Internet Connection, I am showing one Dialog stating that "user does not have internet". Now after switching on the Internet i want onCreate
to be called again and the activity should be recreated.
Please Note: I am switching on the Internet by making dropdown of the top bar of Android and directly switching on the Wifi. How can I achieve this? Should I create a Broadcast Receiver since OnResume
or OnRestart
are not being called while doing this.
Please Help.
This is my Network check Class
public class NetWorkCheck {
public NetWorkCheck(){
}
public static boolean hasConnection() {
ConnectivityManager cm = (ConnectivityManager) Getit.getContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return true;
}
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnected()) {
return true;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
return true;
}
return false;
}
}
This is absolutly perfect.
and in my onCreate
, I am doing like this
net = new NetWorkCheck();
if(net.hasConnection()){
Do task
}else{
Show dialog //I want this dialog to be vanished when user switched on the wifi.and activity should be re created
}
UPDATE 2
Currently, I am using one Broadcast Receiver.
I am making a call after my Dialog like this :->
else{
new InternetNetworkHandle(SplashActivity.this).show();
registerReceiver(
new ConnectivityChangeReceiver(SplashActivity.class),
new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
}
The BroadCast Receiver class
public class ConnectivityChangeReceiver
extends BroadcastReceiver {
String classname;
public ConnectivityChangeReceiver(String classname){
this.classname = classname;
}
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
Log.d("WifiReceiver", "Have Wifi Connection");
act.finish();
Intent refresh = new Intent(context, ???); :-> Here how can i give my that activity name ? and i don't want hardcode.
startActivity(refresh);
}
else
Log.d("WifiReceiver", "Don't have Wifi Connection");
}
};