检查android studio

时间:2015-09-16 06:53:51

标签: java android android-webview mobile-application

我在android studio 1.3.2工作了几天,我对android开发非常新。在这里,我需要检查互联网连接,如果互联网在那里我需要显示一个网页,如果没有互联网,我需要显示一个警告框,说“没有找到互联网连接”。这是我到目前为止所做的代码。

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.activity_main);
        webView.setWebViewClient(new MyWebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        String url = "http://facebook.com";
        webView.getSettings().setJavaScriptEnabled(true);
        if(isConnectingToInternet(getApplicationContext()))   {
            webView.loadUrl(url);
        }else{
            // show alert

            Toast.makeText(getApplicationContext(), "no internet", Toast.LENGTH_LONG).show();
        }
    }



    private boolean isConnectingToInternet(Context applicationContext){
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            // There are no active networks.
            Toast.makeText(getApplicationContext(), "no internet", Toast.LENGTH_LONG).show();
            return false;
        } else
            return true;

    }


    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
  

但是没有互联网连接时警报没有显示。

我启用了

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
AndroidManifest.xml中的

。我被困在这里,我不知道接下来该做什么。我google了很多,但找不到答案。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:2)

把它放在你的Android清单

 <uses-permission android:name="android.permission.INTERNET" />

答案 1 :(得分:1)

检查互联网连接,如果您webview中的已连接加载网址显示提醒。

编辑:您的onCreate()应如下所示:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.sufi_boutique);
        webView.setWebViewClient(new MyWebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        String url = "http://facebook.com";
        webView.getSettings().setJavaScriptEnabled(true);
       if(isConnectingToInternet(getApplicationContext()))   {    
             webView.loadUrl(url);
       }else{
          // show alert
          Toast.makeText(getApplicationContext(),"no internet",Toast.LENGTH_LONG).show();
       }

    }

确保您在清单文件中添加了互联网权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>