Issue
I've searched this and I can't resolve the problem, look at this code checked on other examples:
public class EntryActivity extends Activity {
public Boolean isNetAvailable(Context con) {
try{
ConnectivityManager connectivityManager = (ConnectivityManager)
con.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileInfo =connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
return true;
}
}
catch(Exception e){
e.printStackTrace();
}
return false;
this method works well when I call it in onCreate method, like this:
if (isNetAvailable(getApplicationContext())) {
Toast.makeText(EntryActivity.this,
"You have Internet Connection, wait a moment to sincronize" +
"Metar's stations", Toast.LENGTH_LONG)
.show();
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
} else {
Toast.makeText(EntryActivity.this,
"You Do not have Internet Connection",
Toast.LENGTH_LONG).show();
EntryActivity.this.startActivity(new Intent(
Settings.ACTION_WIRELESS_SETTINGS));
}
This only works for the first Internet connection Check! the problem is, if Wifi is off the user should leaves the app or turns Wi-Fi on.But, if He turns it on, I don't have access to that change and I can't advance to my other activity. So, If in the beggining the internet connection status is down, i want keep checking if the User turns it On to advanve to other activity. What can I do?
Solution
In you android manifest:
<receiver android:name="com.softteco.clivero.receivers.ConnectionReceiver" >
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
And create class:
public class ConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean newConnectionState = isConnected(context);
}
private boolean isConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
boolean ifConnected = false;
for (int i = 0; i < info.length; i++) {
NetworkInfo connection = info[i];
if (connection.getState().equals(NetworkInfo.State.CONNECTED)) {
ifConnected = true;
}
}
return ifConnected;
}
}
It's one of the possible variants. Hope it will help you.
Answered By - Nickolai Astashonok
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.