0

任何人都知道我是否可以在另一个类/对象中接收我的主要活动的 onStop、onPause 和 onResume 回调?

我有一个广播接收器,它位于另一个类(WebView)中。我使用接收器来检测网络何时出现故障并切换到包含一些有用内容的页面的本地副本。我需要在调用 onStop/onPause 时取消注册广播接收器,并在 onResume 期间重新注册它。

我可以手动完成(我在扩展 WebView 的类中添加了几个公共方法来做到这一点),但最好让 Android 为我调用它。

编辑:当然,这是课程,我希望它能够在调用我的主要活动的 onStop 时接收来自 Android 的回调,而无需调用 startInternetMonitoring() / stopInternetMonitoring():

    package com.glimmersoft.spent;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.res.Resources;
    import android.net.ConnectivityManager;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.webkit.WebSettings;
    import android.webkit.WebView;

    /**
     * @author Jer
     *
     */
    public class OfflineWebView extends WebView {

        private BroadcastReceiver receiver;
        private IntentFilter filter;
        private Context myContext;

        public OfflineWebView(Context context,AttributeSet attrs) {
            super(context, attrs);
            WebSettings webSettings = this.getSettings();
            webSettings.setJavaScriptEnabled(true);
            myContext = context;

        }//END CLASS CONSTRUCTTOR

        /**
         * @param internetOn        The URL to display in this OfflineWebView when there is an active Internet connection.
         * @param internetOff       The URL to display in this OfflineWebView if there is no active Internet connection.
         */
        public void setPages(final String internetOn, final String internetOff){

            final OfflineWebView finalThisRef = this;

            filter = new IntentFilter();
            filter.addAction(SpendConstants.ANDROID_CONNECTIVITY_CHANGED);

            receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                   ConnectivityManager cm=(ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);


                   if(cm.getActiveNetworkInfo()!=null&&cm.getActiveNetworkInfo().isConnected()){// TODO: THIS FAILES IF 

                            finalThisRef.loadUrl(internetOn);

                       }else{                      
                            finalThisRef.loadUrl(internetOff);
                       }
                   }//END IF/ELSE

              };

              myContext.registerReceiver(receiver, filter);

        }//END FUNCTION SETPAGES

        public void startInternetMonitoring(){      
            myContext.registerReceiver(receiver, filter);       
        }//END METHOD STARTINTERNETMONITORING

        public void stopInternetMonitoring(){
            myContext.unregisterReceiver(receiver);
        }//END METHOD STOPINTERNETMONITORING

    }//END CLASS OfflineWebView

谢谢大家!

4

1 回答 1

1

与其将您的 BroadcastReceiver 放在您的 OfflineWebView 中,不如让它成为您可能在基本 Activity 中注册的静态类,并让它持有对您的 OfflineWebView 的引用。当 onReceive 被调用时,您可以参考您的 OfflineWebView 来加载您的在线/离线内容。

文件:MyBaseActivity.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.webkit.WebView;

public class MyBaseActivity extends Activity {

    private static final String ANDROID_CONNECTIVITY_CHANGED = "android.net.conn.CONNECTIVITY_CHANGE";
    protected static final ConnectivityBroadcastReceiver sReceiver = new ConnectivityBroadcastReceiver();
    private static final IntentFilter sFilter = new IntentFilter(ANDROID_CONNECTIVITY_CHANGED);

    static class ConnectivityBroadcastReceiver extends BroadcastReceiver {

        private String internetOnUrl = "your online url";
        private String internetOffUrl = "your offline url";
        WebView offlineWebView;

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

            // only do your online/offline loading if we have a webview set
            if (offlineWebView != null) {

                if (cm.getActiveNetworkInfo() != null
                        && cm.getActiveNetworkInfo().isConnected()) {
                    offlineWebView.loadUrl(internetOnUrl);
                } else {
                    offlineWebView.loadUrl(internetOffUrl);
                }
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        // register receiver
        registerReceiver(sReceiver, sFilter);
    }

    @Override
    public void onStop() {
        super.onStop();
        // unregister receiver
        unregisterReceiver(sReceiver);
    }
}

文件:MyActivity.java

import android.R;
import android.os.Bundle;
import android.webkit.WebView;


public class MyActivity extends MyBaseActivity {

    private WebView mWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // load your content root
        setContentView(R.layout.main_layout);

        // find your webview
        mWebView = (WebView)findViewById(R.id.webView);
    }

    @Override
    public void onStart() {
        super.onStart();
        // set your webview in the OfflineBroadcastReceiver
        sReceiver.offlineWebView = mWebView;
    }

    @Override
    public void onStop() {
        super.onStop();
        // clear your webview from the OfflineBroadcastReceiver
        sReceiver.offlineWebView = null;
    }
}
于 2013-01-02T02:38:24.810 回答