• 【Android-java】通过继承的方式实现对webView的相关设置以达到最大降低成本代码改动量的目的


    背景

    在项目中专门有一个意见反馈的activity
    在这里插入图片描述

    现在要求:
    在这里插入图片描述
    就是要统一样式起来:最终形成这样的效果在这里插入图片描述

    问题的难点就在于这个要在原有的代码基础上实现,最好不要简单的在xml上构建。( 原来只是有红色框内的东西),上图是已经实现功能的图样。其他的关于我们等等的都是采用aar包下的某个对象静态方法直接实现的。为了代码统一和实现的效果,最好不要大改。
    在这里插入图片描述
    如何在对aar包改动最小和对原有的上层代码修改最少呢?
    采用继承的方式是最好~

    原有代码-仅供参考学习

    意见反馈的activity:

    @@ -1,126 +0,0 @@
    package im.zego.gocall.personal;
    // file_need_to_be_deleted_for_open_source
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.webkit.JavascriptInterface;
    import android.webkit.WebResourceRequest;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    import androidx.annotation.Nullable;
    
    /**
     * 反馈页面
     */
    public class FeedBackActivity extends BaseActivity {
    
        private WebView mWebView;
        private String feedbackUrl;
        private String TAG = "FeedbackLog";
        private ZegoExpressEngine mZegoExpressEngine;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            ZegoEngineProfile profile = new ZegoEngineProfile();
            profile.appID = KeyCenter.APP_ID;
            profile.appSign = KeyCenter.APP_SIGN;
            profile.scenario = ZegoScenario.GENERAL;
            profile.application = ApplicationHelper.getApplication();
    
            mZegoExpressEngine = ZegoExpressEngine.createEngine(profile, null);
            mWebView = findViewById(R.id.tv_feedbook_webview);
            StringBuilder sb = new StringBuilder();
            sb.append(BackendApiConstants.FEEDBACK_API_URL)
                    .append("/feedback/gocall")
                    .append("/index.html?platform=8")
                    .append("&system_version=android_")
                    .append(android.os.Build.VERSION.RELEASE)
                    .append("&app_version=")
                    .append(BuildConfig.VERSION_NAME)
                    .append("&sdk_version=")
                    .append(ZegoExpressEngine.getVersion())
                    .append("&device_id=")
                    .append(DeviceInfoManager.getAndroidID())
                    .append("&client=")
                    .append(DeviceInfoManager.getModel());
            feedbackUrl = sb.toString();
            loadFeedbackWeb(feedbackUrl);
        }
    
        @Override
        protected int getLayoutId() {
            return R.layout.gocall_activity_feedback;
        }
    
        @Override
        protected boolean isFullScreen() {
            return false;
        }
    
        @Override
        protected boolean isStatusBarTextDark() {
            return true;
        }
    
        public static void actionActivity(Activity activity) {
            Intent intent = new Intent(activity, FeedBackActivity.class);
            activity.startActivity(intent);
        }
    
        private void loadFeedbackWeb(String url) {
            mWebView.loadUrl(url);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setDomStorageEnabled(true);
            mWebView.getSettings().setUseWideViewPort(true);
            mWebView.getSettings().setLoadWithOverviewMode(true);
            mWebView.getSettings().setSupportZoom(false);
            mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    view.loadUrl(request.getUrl().toString());
                    return true;
                }
            });
            mWebView.addJavascriptInterface(new CallAndroid(), "feedback");
        }
    
        class CallAndroid {
            @JavascriptInterface
            public void callback(int result) {
                ZegoAppLog.i(TAG, "提交反馈结果:" + result);
            }
    
            @JavascriptInterface
            public void uploadLog() {
                ZegoAppLog.i(TAG, "开始上传日志");
                mZegoExpressEngine.uploadLog();
            }
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (mZegoExpressEngine != null) {
                ZegoExpressEngine.destroyEngine(null);
            }
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    其中的重点是webview的操作,适应javaScript

     mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    view.loadUrl(request.getUrl().toString());
                    return true;
                }
            });
            mWebView.addJavascriptInterface(new CallAndroid(), "feedback");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    aar中被其他复用的GoWebViewActivity对象

    该aar是由咱自己编包的,所以可以修改。为了安全要保证修改最小。
    用作对webView的操作的对象。

    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Build.VERSION;
    import android.text.TextUtils;
    import android.view.View;
    import android.view.View.OnScrollChangeListener;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.widget.ImageView;
    import android.widget.TextView;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.constraintlayout.widget.ConstraintLayout;
    import im.zego.gologin.R.id;
    import im.zego.gologin.R.layout;
    
    public class GoWebViewActivity extends AppCompatActivity {
        private ConstraintLayout llTitleDefault;
        private ImageView leftIconDefault;
        private TextView tvTitleDefault;
        private ConstraintLayout llTitleBackStyle;
        private ImageView leftIconBlackStyle;
        private TextView tvTitleBlackStyle;
        private WebView webView;
        private String mWebViewUrl;
        public static final int DEFAULT_COLOR_STYLE = 0;
        public static final int BLACK_COLOR_STYLE = 1;
        public static final String URL = "url";
        public static final String TITLE = "title";
        public static final String STYLE = "style";
        public static final String SCROLLABLE = "scrollable";
        private boolean isScrollEnabled = true;
        private String title;
    
        public GoWebViewActivity() {
        }
    
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setContentView(layout.login_activity_webview);
            this.llTitleDefault = (ConstraintLayout)this.findViewById(id.ll_title_default);
            this.webView = (WebView)this.findViewById(id.webView);
            this.mWebViewUrl = this.getIntent().getStringExtra("url");
            this.title = this.getIntent().getStringExtra("title");
            this.isScrollEnabled = this.getIntent().getBooleanExtra("scrollable", true);
            int style = this.getIntent().getIntExtra("style", 0);
            if (TextUtils.isEmpty(this.mWebViewUrl)) {
                this.finish();
            } else {
                this.initDifferStyle(style);
                this.initWebViewLoad();
            }
        }
    
        private void initWebViewLoad() {
            this.webView.loadUrl(this.mWebViewUrl);
            if (VERSION.SDK_INT >= 23 && !this.isScrollEnabled) {
                this.webView.setOnScrollChangeListener(new OnScrollChangeListener() {
                    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                        GoWebViewActivity.this.webView.scrollTo(0, 0);
                    }
                });
            }
    
            WebSettings webSettings = this.webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
            webSettings.setDomStorageEnabled(true);
        }
    
        public WebView getWebView() {
            return this.webView;
        }
    
        private void initDifferStyle(int style) {
            switch(style) {
            case 1:
                this.llTitleBackStyle = (ConstraintLayout)this.findViewById(id.ll_title_back_style);
                this.llTitleDefault.setVisibility(8);
                this.llTitleBackStyle.setVisibility(0);
                this.leftIconBlackStyle = (ImageView)this.findViewById(id.left_icon_black_style);
                this.tvTitleBlackStyle = (TextView)this.findViewById(id.tv_title_black_style);
                this.tvTitleBlackStyle.setText(this.title);
                this.leftIconBlackStyle.setOnClickListener((v) -> {
                    this.finish();
                });
                break;
            default:
                this.leftIconDefault = (ImageView)this.findViewById(id.left_icon_default);
                this.tvTitleDefault = (TextView)this.findViewById(id.tv_title_default);
                this.tvTitleDefault.setText(this.title);
                this.leftIconDefault.setOnClickListener((v) -> {
                    this.finish();
                });
            }
    
        }
    
        public static void jumpActivity(Context context, String url, String title) {
            Intent intent = new Intent(context, GoWebViewActivity.class);
            intent.putExtra("url", url);
            intent.putExtra("title", title);
            context.startActivity(intent);
        }
    
        public static void jumpActivity(Context context, String url, String title, int style) {
            Intent intent = new Intent(context, GoWebViewActivity.class);
            intent.putExtra("url", url);
            intent.putExtra("title", title);
            intent.putExtra("style", style);
            context.startActivity(intent);
        }
    
        public static void jumpActivity(Context context, String url, String title, int style, boolean isScrollEnabled) {
            Intent intent = new Intent(context, GoWebViewActivity.class);
            intent.putExtra("url", url);
            intent.putExtra("title", title);
            intent.putExtra("style", style);
            intent.putExtra("scrollable", isScrollEnabled);
            context.startActivity(intent);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125

    解决

    • 首先将aar包中的webView的属性调整为protected 保证继承后可用
      在这里插入图片描述
    • 在继承之后要实现对webView的更新
      更新之后的反馈类:
    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.JavascriptInterface;
    import android.webkit.WebResourceRequest;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    import androidx.annotation.Nullable;
    
    import im.zego.call.KeyCenter;
    import im.zego.callcommon.log.ZegoAppLog;
    import im.zego.callcommon.provider.ApplicationHelper;
    import im.zego.callcommon.utils.DeviceInfoManager;
    import im.zego.gocall.BackendApiConstants;
    import im.zego.gocall.BuildConfig;
    import im.zego.gologin.login.GoWebViewActivity;
    import im.zego.zegoexpress.ZegoExpressEngine;
    import im.zego.zegoexpress.constants.ZegoScenario;
    import im.zego.zegoexpress.entity.ZegoEngineProfile;
    
    /**
     * 反馈页面
     */
    public class FeedBackActivity extends GoWebViewActivity {
    
        private String TAG = "FeedbackLog";
        private ZegoExpressEngine mZegoExpressEngine;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ZegoEngineProfile profile = new ZegoEngineProfile();
            profile.appID = KeyCenter.APP_ID;
            profile.appSign = KeyCenter.APP_SIGN;
            profile.scenario = ZegoScenario.GENERAL;
            profile.application = ApplicationHelper.getApplication();
    
            mZegoExpressEngine = ZegoExpressEngine.createEngine(profile, null);
            configFeedbackWeb();
        }
    
        public static String getFeedbackUrl() {
            StringBuilder sb = new StringBuilder();
            sb.append(BackendApiConstants.FEEDBACK_API_URL)
                    .append("/feedback/gocall")
                    .append("/index.html?platform=8")
                    .append("&system_version=android_")
                    .append(android.os.Build.VERSION.RELEASE)
                    .append("&app_version=")
                    .append(BuildConfig.VERSION_NAME)
                    .append("&sdk_version=")
                    .append(ZegoExpressEngine.getVersion())
                    .append("&device_id=")
                    .append(DeviceInfoManager.getAndroidID())
                    .append("&client=")
                    .append(DeviceInfoManager.getModel());
            return sb.toString();
        }
    
    
        public static void actionActivity(Activity activity) {
        }
    
        private void configFeedbackWeb() {
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setSupportZoom(false);
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    view.loadUrl(request.getUrl().toString());
                    return true;
                }
            });
            webView.addJavascriptInterface(new CallAndroid(), "feedback");
        }
    
        class CallAndroid {
            @JavascriptInterface
            public void callback(int result) {
                ZegoAppLog.i(TAG, "提交反馈结果:" + result);
            }
    
            @JavascriptInterface
            public void uploadLog() {
                ZegoAppLog.i(TAG, "开始上传日志");
                mZegoExpressEngine.uploadLog();
            }
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (mZegoExpressEngine != null) {
                ZegoExpressEngine.destroyEngine(null);
            }
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    这样就可以对webView操作:

       private void configFeedbackWeb() {
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setSupportZoom(false);
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    view.loadUrl(request.getUrl().toString());
                    return true;
                }
            });
            webView.addJavascriptInterface(new CallAndroid(), "feedback");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 最后那里需要这个反馈类就通过intend的方式调用即可。
      这样咱就是实现了对aar包的最少改动保证了整体的逻辑。
  • 相关阅读:
    【gpt】学习笔记:ChatGPT 用于 3D 内容生成搭配nvidia deepsearch
    手机技巧:推荐一款手机省电、提升流畅度APP
    Spring Security跨站请求伪造(CSRF)
    vue异步加载数据 传递给 子组件,触发mounted方法,子组件修改父组件值
    Google Earth Engine(GEE)——ui.util.debounce的使用
    vue3+ts 实现移动端拖拽交换位置
    レレ / 蕾蕾
    ECMAScript 6(es6)
    25 avl树
    springcloudalibaba架构(17):Gateway网关限流
  • 原文地址:https://blog.csdn.net/weixin_44002043/article/details/126770930