[vue] 안드로이드 javascript bridge 참고 소스

9 views
Skip to first unread message

young chol seo

unread,
Jul 1, 2019, 4:16:38 AM7/1/19
to kamjaroid
참고 https://github.com/Kntt/vue-js-bridge

npm i vue-webview-js-bridge


package com.github.lzyzsd.jsbridge.example;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.github.lzyzsd.jsbridge.DefaultHandler;
import com.google.gson.Gson;
public class MainActivity extends Activity implements OnClickListener {

private final String TAG = "MainActivity";

Context mContext;
BridgeWebView webView;

Button button;

int RESULT_CODE = 0;

ValueCallback<Uri> mUploadMessage;

ValueCallback<Uri[]> mUploadMessageArray;

static class Location {
String address;
}

static class User {
String name;
Location location;
String testStr;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mContext = this;
webView = (BridgeWebView) findViewById(R.id.webView);

button = (Button) findViewById(R.id.button);

button.setOnClickListener(this);

webView.setDefaultHandler(new DefaultHandler());

webView.setWebChromeClient(new WebChromeClient() {

@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) {
this.openFileChooser(uploadMsg);
}

@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType) {
this.openFileChooser(uploadMsg);
}

public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
pickFile();
}

@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
mUploadMessageArray = filePathCallback;
pickFile();
return true;
}
});

webView.loadUrl("http://10.0.2.2:8080");

webView.registerHandler("callNativeHandler", new BridgeHandler() {

@Override
public void handler(String data, CallBackFunction function) {
Log.i(TAG, "handler = callNativeHandler, data from web = " + data);
function.onCallBack("submitFromWeb exe, response data 中文 from Java");
}

});

User user = new User();
Location location = new Location();
location.address = "SDU";
user.location = location;
user.name = "大头鬼";

webView.callHandler("callJsHandler", new Gson().toJson(user), new CallBackFunction() {
@Override
public void onCallBack(String data) {

}
});

webView.send("hello");

}

public void pickFile() {
Intent chooserIntent = new Intent(Intent.ACTION_GET_CONTENT);
chooserIntent.setType("image/*");
startActivityForResult(chooserIntent, RESULT_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == RESULT_CODE) {
if (null == mUploadMessage && null == mUploadMessageArray){
return;
}
if(null!= mUploadMessage && null == mUploadMessageArray){
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}

if(null == mUploadMessage && null != mUploadMessageArray){
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
mUploadMessageArray.onReceiveValue(new Uri[]{result});
mUploadMessageArray = null;
}

}
}

private void showToast(String msg){
Toast.makeText(mContext , "버튼 눌러서 받은값 : " + msg , Toast.LENGTH_SHORT).show();
}

@Override
public void onClick(View v) {
if (button.equals(v)) {
webView.callHandler("functionInJs", "이걸 보낸다 보내 !!", new CallBackFunction() {

@Override
public void onCallBack(String data) {
// TODO Auto-generated method stub
Log.i(TAG, "버튼 눌러서 받은값 " + data);
showToast(data);
}

});
}

}

}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<!-- button 演示Java调用web -->
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:text="@string/button_name"
android:layout_height="48dp"
/>

<!-- webview 演示web调用Java -->
<com.github.lzyzsd.jsbridge.BridgeWebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.github.lzyzsd.jsbridge.BridgeWebView>

</LinearLayout>
src 2222.zip
Reply all
Reply to author
Forward
0 new messages