亂碼1

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);

        // 得到設(shè)置屬性的對象
        WebSettings webSettings = myWebView.getSettings();

        // 使能JavaScript
        webSettings.setJavaScriptEnabled(true);

        // 支持中文,否則頁面中中文顯示亂碼
        webSettings.setDefaultTextEncodingName("GBK");

        // 限制在WebView中打開網(wǎng)頁诽俯,而不用默認(rèn)瀏覽器
        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按鈕按下后調(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: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();//獲得一個方法參數(shù)數(shù)組(getparameterTypes用于返回一個描述參數(shù)類型的Class對象數(shù)組)
                            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();
}
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子晴圾,更是在濱河造成了極大的恐慌译隘,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柑晒,死亡現(xiàn)場離奇詭異,居然都是意外死亡眷射,警方通過查閱死者的電腦和手機(jī)匙赞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來妖碉,“玉大人涌庭,你說我怎么就攤上這事∨芬耍” “怎么了坐榆?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長冗茸。 經(jīng)常有香客問我席镀,道長,這世上最難降的妖魔是什么夏漱? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任豪诲,我火速辦了婚禮,結(jié)果婚禮上挂绰,老公的妹妹穿的比我還像新娘屎篱。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布交播。 她就那樣靜靜地躺著重虑,像睡著了一般。 火紅的嫁衣襯著肌膚如雪秦士。 梳的紋絲不亂的頭發(fā)上缺厉,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機(jī)與錄音伍宦,去河邊找鬼芽死。 笑死,一個胖子當(dāng)著我的面吹牛次洼,可吹牛的內(nèi)容都是我干的关贵。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼卖毁,長吁一口氣:“原來是場噩夢啊……” “哼揖曾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起亥啦,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤炭剪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后翔脱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體奴拦,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年届吁,在試婚紗的時候發(fā)現(xiàn)自己被綠了错妖。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡疚沐,死狀恐怖暂氯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情亮蛔,我是刑警寧澤痴施,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站究流,受9級特大地震影響辣吃,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜芬探,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一齿尽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧灯节,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至形入,卻和暖如春全跨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背亿遂。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工浓若, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蛇数。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓挪钓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親耳舅。 傳聞我的和親對象是個殘疾皇子碌上,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容