package com.qingdaofushan.homecaresas.weiview; import static com.just.agentweb.ActionActivity.REQUEST_CODE; import android.app.Activity; import android.content.Intent; import android.os.Handler; import android.os.Looper; import android.webkit.JavascriptInterface; import com.just.agentweb.AgentWeb; import com.qingdaofushan.homecaresas.activity.LiveActivity; import com.qingdaofushan.homecaresas.event.EventData; import com.qingdaofushan.homecaresas.utils.SharedPreferenceUtil; import com.qingdaofushan.homecaresas.utils.ToastUtil; import com.qingdaofushan.homecaresas.zxing.activity.CaptureActivity; import org.greenrobot.eventbus.EventBus; import org.json.JSONException; import org.json.JSONObject; public class AndroidInterface { private Handler deliver = new Handler(Looper.getMainLooper()); private AgentWeb mAgentWeb; private Activity activity; public AndroidInterface(AgentWeb mAgentWeb, Activity activity) { this.mAgentWeb = mAgentWeb; this.activity = activity; } @JavascriptInterface public void SetData(String json) { deliver.post(new Runnable() { @Override public void run() { try { JSONObject jsonObject=new JSONObject(json); SharedPreferenceUtil.put(activity,jsonObject.getString("key"),jsonObject.getString("value")); // mAgentWeb.getJsAccessEntrace().quickCallJs("SetData","200"); } catch (JSONException e) { ToastUtil.showToast("存储参数报错"); } } }); } @JavascriptInterface public void GetData(String json) { deliver.post(new Runnable() { @Override public void run() { try { JSONObject jsonObject=new JSONObject(json); String key= jsonObject.getString("key"); mAgentWeb.getJsAccessEntrace().quickCallJs("GetData", (String)SharedPreferenceUtil.get(activity,key,"")); } catch (JSONException e) { ToastUtil.showToast("读取参数报错"); } } }); } @JavascriptInterface public void QRCode(String a) { deliver.post(new Runnable() { @Override public void run() { //跳转的默认扫码界面 Intent intent = new Intent(activity, CaptureActivity.class); activity.startActivityForResult(intent, 200); // activity.startActivityForResult(new Intent(activity, CaptureActivity.class),200); } }); } @JavascriptInterface public void StartService(String json) { deliver.post(new Runnable() { @Override public void run() { try { JSONObject jsonObject = new JSONObject(json); String url = jsonObject.getString("url"); Intent intent = new Intent(activity, LiveActivity.class); intent.putExtra("url", url); activity.startActivity(intent); } catch (JSONException e) { ToastUtil.showToast("读取参数报错"); } } }); } @JavascriptInterface public void Camera(String a) { deliver.post(new Runnable() { @Override public void run() { //跳转的拍照 EventBus.getDefault().post(new EventData(3)); } }); } }