package com.witknow.witwebview;
import java.lang.reflect.Method;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import com.witknow.util.StringUtils;
public class witwebviewViewController extends Activity {
private Handler handler= new Handler();
public WebView myWebView = null;
private Button myButton = null;
private String callbackjsfunc;
@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" })
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.witwebview);
myWebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDefaultTextEncodingName("GBK");
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient()
{
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result)
{
return super.onJsAlert(view, url, message, result);
}
});
myWebView.addJavascriptInterface(new WebAppInterface(this),"witwebview");
myWebView.loadUrl("file:///android_asset/index.html");
// 這里用一個Android按鈕按下后調(diào)用JS中的代碼
myButton = (Button) findViewById(R.id.button1);
myButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// 用Android代碼調(diào)用JavaScript函數(shù):
//myWebView.loadUrl("javascript:result('asfas')");
}
});
}
public class WebAppInterface
{
Context mContext;
WebAppInterface(Context c)
{
mContext = c;
}
// 如果target 大于等于API 17叭喜,則需要加上如下注解
@JavascriptInterface
public void postMessage(String json)
{
String num = null,classname="",functioname="",params="";
Class<?> callclass=null;
Object outputobj=null;
try
{
JSONObject objjson=null;
objjson = new JSONObject(json);
classname=objjson.getString("classname");
functioname=objjson.getString("functionname");
params = objjson.getString("params");
callbackjsfunc=objjson.getString("callback");
JSONObject jsonparams=objjson.getJSONObject("params");//params 為json
}
catch(Exception e)
{
Toast.makeText(mContext, "json解析失敗"+e.getMessage().toString(), Toast.LENGTH_SHORT).show();
return;
}
//分析 json
try
{
callclass = Class.forName("com.witknow.witwebview."+classname);
outputobj=callclass.newInstance();//obj指向的A類的對象
//Toast.makeText(mContext, "類存在", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(mContext, "類不存在", Toast.LENGTH_SHORT).show();
return;
}
Class[] paramTypes = null;
try
{
Method[] fields = outputobj.getClass().getDeclaredMethods();//獲取對象屬性
for(int z=0;z<fields.length;z++)
{
Method field=fields[z];
if(field.getName().toString().equals("output"))
{
paramTypes = field.getParameterTypes();//獲得一個方法參數(shù)數(shù)組(getparameterTypes用于返回一個描述參數(shù)類型的Class對象數(shù)組)
break;
//for(int j = 0 ; j < paramTypes.length; j++)
//{
//Toast.makeText(getApplicationContext(), paramTypes[j].getName().toString(), Toast.LENGTH_SHORT).show();
//Toast.makeText(mContext,paramTypes[ j ].getName(), Toast.LENGTH_SHORT).show();
//}
//break;
}
}
Method method = callclass.getMethod("output",paramTypes);//初始化方法
final Object obj=method.invoke(outputobj,"dfgHJKL:FGHJKL:");
handler.post(new Runnable() {
@Override
public void run(){
returnjs(obj);
}
});
}
catch(Exception e)
{
Toast.makeText(mContext, "方法不存在"+e.getMessage().toString(), Toast.LENGTH_SHORT).show();
return;
}
}
}
public void returnjs(Object params)
{
myWebView.loadUrl("javascript:result('"+params.toString()+"')");
}
public String output(String str)
{
return str;
}
}