This is a very genuine case and will cause trouble when your application tries to access internet in an organizational body like in an institute or company which uses proxy for letting users to connect to internet.
As I have not received any answer on this question I am going to tell the procedure followed by me for checking this, as sometimes for your application to be unable to access internet can make it dead.
At first I check if internet connection is available on the device by simple state checking of the device using this
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED) {
//Do what ever you wish to do
} else {
// Display message internet connection not available
}
The above code gives information on whether internet connection is available or not but it fails to tell if the internet connection requires authentication (hence leading to nothing getting downloaded or corrupted file or zero size file to be created in the download file location), hence after checking the above condition I try to access a link to download and then use this logic.
I have provided a link to a small .zip file from internet which my application tries to download when a button to access internet is pressed (like user clicking on "update the database" of application). I have used the information given in this question ( Download and Extract Zip File in Android ), and then I extract the downloaded file in a unzipping location.
After this I have created a check option (being already aware of exact name of the file that exists inside the .zip file), which checks whether this file has been created in the unzipping location mentioned by me while unzipping.
If the file does not exist, you just give them an option to open browser and provide the authentication and then return back to your application like such:
private void unableToConnectToInternetDialog() {
// TODO Auto-generated method stub
String Message ="CHECK IF:" +
"your internet connection uses proxy, if yes please follow these steps:" +
"\n 1 Open default Internet Browser" +
"\n 2 Open any webpage requiring authentication" +
"\n 3 Provide the authentication" +
"\n 4 Resume this application";
// TODO Auto-generated method stub
AlertDialog.Builder builderInternet = new AlertDialog.Builder(AppPage1.this);
builderInternet.setMessage(Message)
.setTitle("Unable to connect to Internet")
.setCancelable(false)
.setPositiveButton("Open Browser", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Opening mobile's default browser to set the authentication
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
})
.setNegativeButton("Skip Update", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do the needful
}
});
builderInternet.create().show();
}
}