WebView.....

webView最好放另外一个进程 优点1:风险隔离,web挂了不影响app 优点2:可以让linux给app申请跟多的内存

多进程通信主要是aidl , aidl 主要就是 要拥有一个 web2Main的一个接口, 当我们点击web上的东西时候。回传给app(这个web2Main的接口可以包含一个main2Web的callback的回调,方便web--->app---->web 这一套流程的传递)

以下操作:使用命令模式+接口下沉进行,简化操作
1.可以再web的模块中,定义一个Command接口{ String name , void execute(....) },再App模块实现,
2.使用google提供的AutoService 自定义注解库,对标记Command注解的类进行,自动解析
ServiceLoader serviceLoader = ServiceLoader.load(Command.class);
这个方法可以拿到所有Comannd的注解类,然后遍历,按照name执行对应的 excute()

要求web的html中的name 和Command实现类当中的name 一样

webView 一些简单操作:
1WebViewDefaultSettings 一些默认的Settings 网上可以百度到
2WebChromeClient 可以实现打日志功能onConsoleMessage , 设置标题onReceivedTitle 等等
3WebViewClient 可以onPageStarted,onPageFinished,onReceivedError等响应
4WebView 可以 addJavascriptInterface(this, "AAAA")为web增加名字,方便html,js调用
@JavascriptInterface 注解到具体的方法,方便执行html,js中的回调

==========准备部分如下:=======================================
物料:
1.再Manifest中声明一个MainCommandService服务,伴随运行
2.MainCommandService----->onBind()---->绑定一个MainCommandsManager
3.1MainCommandsManager 实现 web2mainInterface.Stub 的web2main的aidl接口
3.2MainCommandsManager 构造方法中通过以下方法将命令收集起来
ServiceLoader serviceLoader = ServiceLoader.load(Command.class)
mCommands.put(command.name(), command);
4,a再BaseWebView.init()中--->WebCommandDispatcher.initAidlConnection()
b--->application和MainCommandService绑定,
c----->再onServiceConnected 获取web2mainAidl = web2mainInterface.Stub.asInterface(service);
这就保证了 MainCommandService服务不挂我web2mainAidl 也不为null
========下面流程部分:==============================================
流程:
// step1 ,html--点击按钮--->webview.js--->BaseWebView中的takeNativeAction
---->web2mainAidl.handleWebCommand------->MainCommandsManager.handleWebCommand--->进行分发------>MainCommandsManager中通过 mCommands.get(commandName).execute(params, callback);进行匹配执行----->回调到web2mainAidl中Callbackmain2web中------>baseWebView.handleCallback
--->evaluateJavascript最终到WebView之中 。完成一次 web-->main-->web的操作

你可能感兴趣的:(WebView.....)