h5加android原生开发越来越普及了,可是android的webview一直都是问题多多,可以考虑用第三方,在网上找了一下发现两个

腾讯的TBS

Crosswalk

TBS官网有教程,也没有研究,就研究了Crosswalk

下载

我用的android studio版本是3.0.1,其它版本导入应该方法大同小异

先在两个build.gradle内分别添加

buildscript {        repositories {        maven {            url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'        }        google()        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:3.0.1'                // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}allprojects {    repositories {        maven {            url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'        }        google()        jcenter()    }}task clean(type: Delete) {    delete rootProject.buildDir}
dependencies {    implementation fileTree(dir: 'libs', include: ['*.jar'])    implementation 'com.android.support:appcompat-v7:27.0.2'    implementation 'com.android.support.constraint:constraint-layout:1.0.2'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'com.android.support.test:runner:1.0.1'    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'    implementation 'org.xwalk:xwalk_core_library:23.53.589.4'}

activity_main.xml

MainActivity.java

public class MainActivity extends AppCompatActivity {    private XWalkView walkView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);        walkView = findViewById(R.id.walk_view);        walkView.loadUrl("http://www.baidu.com");    }    @Override    protected void onPause() {        super.onPause();        if (walkView != null) {            walkView.pauseTimers();            walkView.onHide();        }    }    @Override    protected void onResume() {        super.onResume();        if (walkView != null) {            walkView.resumeTimers();            walkView.onShow();        }    }    @Override    protected void onDestroy() {        super.onDestroy();        if (walkView != null) {            walkView.onDestroy();        }    }    @Override    protected void onNewIntent(Intent intent) {        if (walkView != null) {            walkView.onNewIntent(intent);        }    }}

效果就不看了,就是浏览器的样子

试试js和java交互

新建一个html文件

java代码

放入assets文件夹

MainActivity.java内修改代码

walkView = findViewById(R.id.walk_view);walkView.addJavascriptInterface(new JsInterface(), "WebToJava");walkView.loadUrl("file:///android_asset/index.html");

新建个类

public class JsInterface {    public JsInterface() {    }    @JavascriptInterface    public String helloJava() {        return "这是一段java代码";    }}

@JavascriptInterface是org.xwalk.core.JavascriptInterface别搞错成webview的

好了,看效果

android webview的代替品_第1张图片

最后还要说一句,用Crosswalk体积会爆增,如果大量用到h5的话可以考虑使用,听说使用分开打包的方式能减少体积

 

 

 

 

 

更多相关文章

  1. Android 动态加载APK--代码安装、获取资源及Intent调用已安装apk
  2. Android 利用JNI调用Android Java代码函数
  3. Android浮动窗口实现原理及代码实例
  4. ubuntu下载android源代码
  5. Android 权限控制代码分析
  6. Android初次使用遇到的两个问题

随机推荐

  1. Android:控件ProgressBar进度条
  2. android自定义带图片的title
  3. android数据库操作
  4. 开源项目收集整理
  5. android 4.2 compile on 64 bits Ubuntu
  6. android:duplicateParentState="true"
  7. Android仿QQ圆形头像
  8. android 模拟器 得到GPS
  9. Android特色开发(5):账户管理
  10. 关于Android WebView上传文件的解决方案