android显示本地html虽然没有连接,如果连接比加载web url

时间:2015-02-20 08:46:24

标签: java android

我们有一个加载远程网址的常规webview。 “虽然”没有互联网连接我们想要加载本地html文件,并且每3秒。我们必须再次检查是否有连接。如果是真的,我们应该加载远程URL。

我们已经有一个检查连接的类,并给我们一个布尔值

有没有办法用do while检查这个?

我们的代码如下所示:

    // creating connection detector class instance
    cd = new ConnectionDetector(getApplicationContext());

    // CHECK INTERNET CONNECTION
    isInternetPresent = cd.isConnectingToInternet();


    // Check if connected to internet (true or false)
    if (isInternetPresent) {
    mWebView.loadUrl("http://xxxx.xxxx.xx.xx/xxxxx/#/?h=" + xxx + "&location=" + locationName);

    }
    else {
    // if no connection
    mWebView.loadUrl("file:///android_asset/check.html");

    // HERE WE HAVE TO CHECK EVERY 3 SEC. IF THERE IS CONNECTION AGAIN AND LOAD TEHE REMOTE URL
    // PERHAPS A WAY TO START THE CODE AGAIN FROM LINE ABOVE isInternetPresent = cd.isConnectingToInternet();

    }

    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

1 个答案:

答案 0 :(得分:1)

为什么不注册监听连接更改的BroadcastReveiver

public class NetworkStateChangedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable() || mobile.isAvailable()) {
            LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(YourWebViewActivity.NETWORK_STATE_CHANGED));
        }
    }
}

并将其注册到AndroidManifest.xml文件中:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >
  <receiver android:name=".NetworkStateChangedReceiver" >
    <intent-filter>
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
      <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
  </receiver>
</application>

修改您的活动以包含此逻辑:

public static final String NETWORK_STATE_CHANGED = "yourpackagename.action.networkStateChanged";

private WebView mWebView;

private LocalBroadcastManager mBroadcastManager;
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(NETWORK_STATE_CHANGED)) {
      // Here is your logic for loading the real page.
      mWebView.loadUrl("http://xxxx.xxxx.xx.xx/xxxxx/#/?h=" + xxx + "&location=" + locationName);
    }
  }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.your_xml);
  mWebView = (WebView) findViewById(R.id.your_webview_id);
  mBroadcastManager = LocalBroadcastManager.getInstance(this);
  IntentFilter intentFilter = new IntentFilter();
  intentFilter.addAction(NETWORK_STATE_CHANGED);
  mBroadcastManager.registerReceiver(mBroadcastReceiver, intentFilter);
}

@Override
protected void onDestroy() {
  super.onDestroy();
  mBroadcastManager.unregisterReceiver(mBroadcastReceiver);
}        

这样您就可以避免使用轮询机制来检查网络状态。

希望这会有所帮助:)