如何在另一个类的片段中命令WebView?

时间:2017-01-27 20:07:00

标签: android webview fragment

基本上我在一个片段中有一个WebView,并且有一个java类来检查网络可用性。我可以为真假条件制作祝酒词。但是由于WebView在另一个片段中,我无法加载URL。因此我的问题是: 如何从另一个班级调用项目?

编辑:

package com.sahin.spm;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
import layout.fragHome;

/**
 * Created by PD on 27.1.2017.
 */
public class NetworkChangeReceiver extends BroadcastReceiver{
    private static final String LOG_TAG = "Connection control";
    static boolean isConnected = false;

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

        isNetworkAvailable(context);

    }


    private boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {

                        if(!isConnected){ 
                            isConnected = true;
                            Toast.makeText(context, "Connected!", Toast.LENGTH_LONG).show();
                            fragHome.loadWebViewUrl("http://www.google.com.tr");

                        }
                        return true;
                    }
                }
            }
        }
        isConnected = false;
        Toast.makeText(context, "No connection!", Toast.LENGTH_LONG).show();
        fragHome.loadWebViewUrl("file:///android_asset/noconnection.html");
        return false;
    }
}

这是我的类,我在其中控制连接并希望webview根据连接状态加载页面。没有错误,但这种方式无法加载。

fragHome.java

package layout;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.sahin.spm.NetworkChangeReceiver;
import com.sahin.spm.R;


public class fragHome extends Fragment {
    private static final String LOG_TAG = "Auto control";
    private NetworkChangeReceiver receiver;
    private static WebView webView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_frag_home, container, false);

        final WebView webView = (WebView) rootView.findViewById(R.id.hWebView);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return super.shouldOverrideUrlLoading(view, request);
            }
        });


        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        receiver = new NetworkChangeReceiver();
        getActivity().registerReceiver(receiver, filter);


        return rootView;

    }
    public static void loadWebViewUrl(String url) {
        if (webView != null) {
            webView.loadUrl(url);
        }
    }
    @Override
    public void onDestroy() { 
        super.onDestroy();

        getActivity().unregisterReceiver(receiver);

    }

}

2 个答案:

答案 0 :(得分:0)

使用LocalBroadcastManager或EventBus库。

在webview片段上只监听广播/事件并加载网址。

答案 1 :(得分:-1)

简单的方法是使用静态WebView并使用静态方法或直接调用其URL的加载。

修改

public class WebViewerFragment extends Fragment {

    private View rootView;
    private static WebView webView;

    public WebViewerFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, 
                             ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_web_viewer, container, false);

        WebView webView = (WebView) rootView.findViewById(R.id.webview);

        return rootView;
    }

    public static void loadWebViewUrl(String url) {
        if (webView != null) {
            webView.loadUrl(url);
        }
    }
}

这样您可以从以下任何地方调用loadWebViewUrl方法:

WebViewerFragment.loadWebViewUrl(desiredUrl);

但重要的是你应该谨慎webView实例不应该是null,因此在调用此方法之前必须初始化WebViewerFragment