package com.witknow.witcontact;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.json.JSONObject;
import com.witknow.util.StringUtils;
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.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;
public class witwebviewViewController extends Activity {
private 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.activitywitwebview);
myWebView = (WebView) findViewById(R.id.myWebView);
// 得到设置属性的对象
WebSettings webSettings = myWebView.getSettings();
// 使能JavaScript
webSettings.setJavaScriptEnabled(true);
// 支持中文,否则页面中中文显示乱码
webSettings.setDefaultTextEncodingName("GBK");
// 限制在WebView中打开网页,而不用默认浏览器
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient()
{
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result)
{
// TODO Auto-generated method stub
return super.onJsAlert(view, url, message, result);
}
});
myWebView.addJavascriptInterface(new WebAppInterface(this),
"witwebview");
myWebView.loadUrl("file:///android_asset/index.html");
// 这里用一个Android按钮按下后调用JS中的代码
myButton = (Button) findViewById(R.id.button1);
myButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// 用Android代码调用JavaScript函数:
myWebView.loadUrl("javascript:myFunction()");
}
});
}
public class WebAppInterface
{
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c)
{
mContext = c;
}
/** Show a toast from the web page */
// 如果target 大于等于API 17,则需要加上如下注解
@JavascriptInterface
public void postMessage(String json)
{
String num = null,classname="",functioname="",params="";
Class callclass=null;
Object obj=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");
Object my=null;
my=objjson.get("params");
//Toast.makeText(getApplicationContext(), params, Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), my.getClass().toString(), Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
//Toast.makeText(getApplicationContext(),e.getMessage().toString(), Toast.LENGTH_SHORT).show();
return;
}
//分析 json
try
{
callclass = Class.forName("com.witknow.witcontact."+classname);
obj=callclass.newInstance();//obj指向的A类的对象
//Toast.makeText(mContext, "类存在", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
//Toast.makeText(mContext, "类不存在", Toast.LENGTH_SHORT).show();
return;
}
try
{
Method[] fields = obj.getClass().getDeclaredMethods();//获取对象属性
for (Method field : fields) {
if(field.getName().toString().equals("output"))
{
Class[] paramTypes = field.getParameterTypes();//获得一个方法参数数组(getparameterTypes用于返回一个描述参数类型的Class对象数组)
for(int j = 0 ; j < paramTypes.length ; j++)
{
Toast.makeText(mContext,paramTypes[ j ].getName(), Toast.LENGTH_SHORT).show();
}
}
}
//Method method = callclass.getDeclaredMethod("output");
Object[] objs=null;
Class[] argsClass = new Class[2];
argsClass[0]="方法不存在".getClass();
argsClass[1]="方法不存在".getClass();
Method method = obj.getClass().getMethod("output",argsClass);
//Toast.makeText(mContext, "方法存在".getClass().toString(), Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(mContext, "方法不存在".getClass().toString(), Toast.LENGTH_SHORT).show();
return;
}
if(!StringUtils.isMobileNO(num))
{
return;
}
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" +num);
intent.setData(data);
startActivity(intent);
// Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
//Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show();
}
}
private void output(String str,String str2,int age,Integer phone)
{
Toast.makeText(getApplicationContext(), str+","+str2, Toast.LENGTH_SHORT).show();
}
}