连接恢复后WebView重新加载?

时间:2015-05-07 14:15:51

标签: java android webview broadcastreceiver

[已解决] [以下固定代码]

这是一个带有webview的小应用程序,我希望在连接到互联网时重新加载。我打开应用程序(连接互联网),它显示网络(google.com.ar),当我断开WiFi和3G,如果我在网络上导航它显示错误消息,它没关系,但如果我重新连接WiFi应用程序崩溃。问题是什么?我认为是在BroadReceiver中,在清单中我有android.net.wifi.STATE_CHANGE和android.net.conn.CONNECTIVITY_CHANGE。

感谢。

MainActivity.java

package com.example.app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends ActionBarActivity {

    static WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.enableDefaults();
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myWebView = (WebView) findViewById(R.id.webView);
        myWebView.loadUrl("http://google.com.ar");

        myWebView.setWebViewClient(new WebViewClient() );

    }

}

ConnectionBroadReceiver.java

package com.example.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionBroadReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager cm = (ConnectivityManager)    
        context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo netInfo = cm.getActiveNetworkInfo();

        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            MainActivity.myWebView.reload();
        } else {
        }

    }
}

的AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden">
        <activity
            android:name="com.example.app.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name="com.example.app.ConnectionBroadReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

0 个答案:

没有答案