Force close window in Async task execute [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Force close window, When Async task execute after connection is loss.
I'm tired using ConnectivityManager to check internet connection, because when device connect to wifi but no internet connection, ConnectivityManager showing is connected. can any body help me?
public boolean isConnected(){
boolean status=false;
ConnectivityManager cManager=(ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] allNetworkInfo = cManager.getAllNetworkInfo();
for (int i = 0; i<allNetworkInfo.length; i++){
if (allNetworkInfo[i].getState() == NetworkInfo.State.CONNECTED){
status = true;
}
}
return status;
}

you are almost done but need to check it also that network is connecting or connected.
public boolean isConnected(){
boolean status=false;
ConnectivityManager cManager=(ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] allNetworkInfo = cManager.getAllNetworkInfo();
for (int i = 0; i<allNetworkInfo.length; i++){
if (allNetworkInfo[i].isConnectedOrConnecting()){
status = true;
}
}
return status;
}
I Hope it helps you...

You can try this code to see if it works:
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

Try this code it works for me.
public static boolean isConnectedWithInternet(Context context) {
// return true;
boolean _isNetAvailable = false;
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null) {
_isNetAvailable = wifiNetwork.isConnectedOrConnecting();
}
NetworkInfo mobileNetwork = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null) {
_isNetAvailable = mobileNetwork.isConnectedOrConnecting();
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
_isNetAvailable = activeNetwork.isConnectedOrConnecting();
}
// CGlobalVariables._isInternetOn = _isNetAvailable;
return _isNetAvailable;
}

You generally get an UnknownHostException when Internet is not reachable.
You should always check connected state before starting the request. But to handle connection loss during the process you can always catch UnknownHostException.

Related

Check if connected to specific wifi on app start

When my app starts I want to check if the device is connected to a specific wifi, my current code looks like this
private boolean haveNetwork() {
boolean have_WIFI = false;
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
for (NetworkInfo info:networkInfos) {
if (info.getTypeName().equalsIgnoreCase("WIFI" )) {
if (info.isConnected())
have_WIFI = true;
}
}
return have_WIFI;
}
But it only checks if the device has any WIFI connection at all. How would I modify it to check for a specific WIFI SSID e.g. "Home"?

How to choose the internet connection mode Android

I made an Android application that is used to exchange files with a pc.
The computer and the device are connected to the same network and everything worked very well.
But since I put a 4g sim card, the transfer is no longer done, does anyone know how to solve this problem?
There must be a way to force my application to use only ethernet connection?
Permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
How I check connection:
public static Boolean isConnected(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = null;
if (cm != null) {
activeNetwork = cm.getActiveNetworkInfo();
}
boolean isEthernet = false;
if (activeNetwork != null) {
isEthernet = activeNetwork.getType() == ConnectivityManager.TYPE_ETHERNET;
}
return activeNetwork != null &&
activeNetwork.isConnectedOrConnecting() && isEthernet;
}
Main function:
public void sendFilesInFolder(Boolean bool) {
if (isConnected(MainActivity.this)) {
ProgressBar progressBar;
if (bool) progressBar = mProgressBarD;
else progressBar = mProgressBarM;
fileSender = new FileSender(prefs.getString("ip_adress", null),
Integer.valueOf(prefs.getString("port_dest", null)),
bool, colisageBase, progressBar);
fileSender.execute();
} else {
Toast.makeText(MainActivity.this, getString(R.string.no_connection), Toast.LENGTH_SHORT).show();
}
}

App stops when implementing ConnectivityManager and NetworkInfo

For my app, I was trying to detect when the user is connected to the internet. This is one of the methods I've been using, although I've used some other that where basically the same, but using other methods of the objects used:
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected())
{
return true;
}
return false;
}
Even if this does not lead to any compilation error, when launching the app it stops abruptly and does not respond. I'd like to know why this happens and how to solve it or, if has no aparent solution, if there's another way of doing what I wanted.
I have tried deleting part of the code strategically and I have noticed that the part that gives problems is netInfowith both of ways it is being used, but I don`t know why.
Add Access Network Permission in AndroidManifest file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
First add users-permission in AndroidManifest.xml file like this
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Second, this pics of code can give you a boolean. Network connection is available or not. so put it in a method and get return of those code like this..
public boolean getCheckedNetStatus(Context context) {
try{
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
} else {
return false;
}
}catch(Exception e){
Log.e("Net Check Err", e.toString());
return false;
}
}
Have try catch will protect your from unexpected crash your app...
Now Call it to conditionally execute your certain action like this
if(getCheckedNetStatus(yourActivity.this)){
//Net is available
//So do your action
}else{
//Net not available
//you can notify user that net not available
//So you (User) can't do this
}

How check Internet-connection in Android? [duplicate]

This question already has answers here:
How do you check the internet connection in android? [duplicate]
(8 answers)
Closed 8 years ago.
What are the ways to check your Internet connection in Android.
And what is the difference between them?
Create method like below.
public static boolean isConnectingToInternet(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
and call it wherever you want. Put it in IF - ELSE condition so you can check for connection.
Or if you want to call this method through out your project then you can create one static class like below.
public class ConnectivityDetector {
public static boolean isConnectingToInternet(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
}
and call it like below shown code in every class wherever you want.
if (ConnectivityDetector.isConnectingToInternet(Test.this)) {
// do your stuff
} else {
// Alert dialog that says, No Internet connection
}
Also make sure that you have the required permission to monitor the network state. You need to add this permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Hope this will help you.

How to detect if user has 3G/Wifi on before making URL connection?

I don't want my app to crash if the user doesn't have wifi or 3g connectivity. How can I catch this at runtime in my app?
First get a reference to the ConnectivityManager and then check the Wifi and 3G status of the device.
You'll need the ACCESS_NETWORK_STATE permission to use this service.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mWifi.isConnected() == false && mMobile.isConnected() == false) {
showDialog(DIALOG_NETWORK_UNAVAILABLE);
}
connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) is deprecated.
Below is an improved solution for the latest Android SDK.
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
boolean is3gEnabled = false;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Network[] networks = connManager.getAllNetworks();
for(Network network: networks)
{
NetworkInfo info = connManager.getNetworkInfo(network);
if(info!=null) {
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
is3gEnabled = true;
break;
}
}
}
}
else
{
NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(mMobile!=null)
is3gEnabled = true;
}

Categories