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

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的话可以考虑使用,听说使用分开打包的方式能减少体积

 

 

 

 

 

你可能感兴趣的:(android)