Reputation: 235
I know that this question may have been asked before, but i can't figure out how to use those answers to make them work with my app.
I want to check if there is an internet connection when the user of the app opens the app. I've found some methods to check that, but i'm totally new to android developing and i just don't know how to do it. This is my current application file:
package com.pocket.line;
import com.pocket.line.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class AndroidMobileAppSampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("http://www.google.com/");
}
private class MyCustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
If there is no connectivity, i would like to force the app to close, and show an alert. Snippets of code i've found that can check this:
public boolean hasActiveInternetConnection()
{
try
{
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(4000);
urlc.setReadTimeout(4000);
urlc.connect();
networkcode2 = urlc.getResponseCode();
return (urlc.getResponseCode() == 200);
} catch (IOException e)
{
Log.i("warning", "Error checking internet connection", e);
return false;
}
}
I just don't know what parts of that code i need to change to make it work with my app. Any help would be appreciated!
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pocket.line" android:versionCode="3"
android:versionName="1.2">
<uses-sdk android:minSdkVersion="11" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/ic_launcher" android:label="Pocket Line">
<activity android:name="com.pocket.line.AndroidMobileAppSampleActivity"
android:label="Pocket Line">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 4
Views: 10007
Reputation: 48602
I think you have not declare the activity in AndroidManifest.xml
Do like this If this activity is a launcher of application.
<activity
android:name=".AndroidMobileAppSampleActivity"
android:label="@string/title_activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If this activity is not a launcher.
<activity
android:name=".AndroidMobileAppSampleActivity"
android:label="@string/title_activity" >
</activity>
Instead of this checking internet connection do like this.
/**
* This method check mobile is connected to network.
* @param context
* @return true if connected otherwise false.
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(conMan.getActiveNetworkInfo() != null && conMan.getActiveNetworkInfo().isConnected())
return true;
else
return false;
}
and Add permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
and check this by using the following code:
if(!isNetworkAvailable(this)) {
Toast.makeText(this,"No Internet connection",Toast.LENGTH_LONG).show();
finish(); //Calling this method to close this activity when internet is not available.
}
Upvotes: 7
Reputation: 11310
There is another simple way to do that
public class MyActivity extends Activity {
// flag for Internet connection status
Boolean isInternetPresent = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get Internet status
isInternetPresent = isConnectingToInternet;
if (!isInternetPresent) {
showAlertDialog(MyActivity.this, "No Internet Connection",
"You don't have internet connection.", false);
}
}
/**
* check the Internet connection and return true or false
*
* @return
*/
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext()
.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;
}
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
alertDialog.setIcon(R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finished();
}
});
// Showing Alert Message
alertDialog.show();
}
}
Upvotes: 1
Reputation: 14332
check like this
if(hasActiveInternetConnection()){
mainWebView.loadUrl("http://www.google.com/");
}else{
Log.d("TAG","No Internet connection");
}
Hope this will helps you.
Upvotes: 1