Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force to close app when no internet connection available

Tags:

android

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>
like image 970
Yannick Avatar asked Feb 17 '23 23:02

Yannick


2 Answers

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.
}
like image 104
Ajay S Avatar answered Mar 05 '23 20:03

Ajay S


check like this

 if(hasActiveInternetConnection()){
        mainWebView.loadUrl("http://www.google.com/");
    }else{
        Log.d("TAG","No Internet connection");
    }

Hope this will helps you.

like image 43
Amol Sawant 96 Kuli Avatar answered Mar 05 '23 20:03

Amol Sawant 96 Kuli