android webview的代替品

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

<org.xwalk.core.XWalkView
    android:id="@+id/walk_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

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文件

<!DOCTYPE html>
<html>
<body>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<a href="#" onclick="clicked()">java代码</a>
<script>
function clicked() {
   alert(WebToJava.helloJava());
}
</script>

</body>
</html>

放入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的

好了,看效果

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

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值