Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中检查互联网连接?_Android - Fatal编程技术网

如何在android中检查互联网连接?

如何在android中检查互联网连接?,android,Android,我有这个代码和工作,但我不能把代码检查互联网 我不会检查互联网,如果可用,应用程序转到我的URL,如果不显示消息 “没有互联网可用”。代码运行得很好,但我无法在连接后为我找到URL code1 HelooWebViewActivity.java package xonmp.hellowebview; import android.os.Bundle; import android.annotation.SuppressLint; import android.a

我有这个代码和工作,但我不能把代码检查互联网 我不会检查互联网,如果可用,应用程序转到我的URL,如果不显示消息 “没有互联网可用”。代码运行得很好,但我无法在连接后为我找到URL

code1
   HelooWebViewActivity.java

   package xonmp.hellowebview;

   import android.os.Bundle;
   import android.annotation.SuppressLint;
   import android.app.Activity;
   import android.view.KeyEvent;
   import android.view.Menu;
   import android.webkit.WebView;
   import android.webkit.WebViewClient;

   @SuppressLint("SetJavaScriptEnabled") public class HelloWebViewActivity 
    extends Activity {


    private WebView mWebView;


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



    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("http://www.google.com");
    mWebView.setWebViewClient(new HelloWebViewClient());

   }

   private class HelloWebViewClient extends WebViewClient {

   // @Override
    public boolean shoudOverridUrlLoading(WebView webview, String url)
    {
        webview.loadUrl(url);
        return true;

    }

  }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
    {
    mWebView.goBack();
    return true;
        }
      return super.onKeyDown(keyCode, event);

   }
   }



  code 2
  AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="xonmp.hellowebview"
  android:versionCode="1"
  android:versionName="1.0" >

   <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="xonmp.hellowebview.HelloWebViewActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   </application>
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

  </manifest>


  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="xonmp.hellowebview"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="xonmp.hellowebview.HelloWebViewActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   </application>
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

   </manifest>




  code 3

  Activity_heloo_web_view.xml

  <WebView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/webview"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".HelloWebViewActivity" 
  />
code1
HelooWebViewActivity.java
包装xonmp.hellowebview;
导入android.os.Bundle;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.view.KeyEvent;
导入android.view.Menu;
导入android.webkit.WebView;
导入android.webkit.WebViewClient;
@SuppressLint(“SetJavaScriptEnabled”)公共类HelloWebViewActivity
扩展活动{
私有网络视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u hello\u web\u视图);
mWebView=(WebView)findViewById(R.id.WebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(“http://www.google.com");
setWebViewClient(新的HelloWebViewClient());
}
私有类HelloWebViewClient扩展了WebViewClient{
//@覆盖
公共布尔值shoudverridulloading(WebView WebView,字符串url)
{
loadUrl(url);
返回true;
}
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件)
{
if((keyCode==KeyEvent.keyCode_BACK)&&mWebView.canGoBack())
{
mWebView.goBack();
返回true;
}
返回super.onKeyDown(keyCode,event);
}
}
代码2
AndroidManifest.xml
代码3
活动\u heloo\u web\u view.xml

像这样的东西应该可以排序

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
您还需要在您的清单中使用此项

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

此代码可能会帮助您:

public boolean isConnectionAvailable(Context context) {
    final ConnectivityManager connMgr = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileNetWork = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    return wifi.isConnected() || mobileNetWork.isConnected();
  }
使用类似以下内容:

if(isConnectionAvailable(getApplicationContext()) {
    mWebView.loadUrl("http://www.google.com");
    mWebView.setWebViewClient(new HelloWebViewClient());
} else {
//Toast message 
Toast.makeText(getApplicationContext(), " No internet avalaible", Toast.LENGTH_SHORT).show();
}
在menifest内部,将以下权限放在应用程序标记之外

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

首先,您需要在清单中声明您需要访问网络状态,您可以这样做:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
ConnectivityManager manager 
      = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
然后,要获取wifi状态,您可以使用以下命令:

NetworkInfo wifiStatus = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
和移动网络

NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
要获取网络的状态,可以使用以下方法

isConnected();
并在一个方法中使用它们:比如说CheckConnectionAvailable

public boolean CheckConnectionAvailable(){

  ConnectivityManager manager = (ConnectivityManager) context
    .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return wifi.isConnected() || mobile.isConnected();
}
已编辑

如果要使用按钮刷新页面,有两种方法

但我将向您展示如何在xml中实现这一点,因为这样可以使代码更干净,并将代码与布局分离。比如说,这个按钮是用xml声明的

<Button id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Refresh Page" android:onClick="onClick"/>
如果我在没有internet可用于刷新页面时不添加按钮(重试)?如果我在没有internet可用于刷新页面时不添加按钮(重试)??
public void onClick(View view){
   switch(view.getId()){
    case R.id.button:
     boolean isAvailable = CheckConnectionAvailable();
     if(isAvailable){
        Toast.makeText(this,"message you wanna show if theres a connection available", Toast.LENGTH_SHORT).show();    
      }else {
  Toast.makeText(this,"message you wanna show if theres not a connection available",
  Toast.LENGTH_SHORT).show();
    }
     break;
  }
}