AndroidInterface.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.qingdaofushan.homecaresas.weiview;
  2. import static com.just.agentweb.ActionActivity.REQUEST_CODE;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Handler;
  6. import android.os.Looper;
  7. import android.webkit.JavascriptInterface;
  8. import com.just.agentweb.AgentWeb;
  9. import com.qingdaofushan.homecaresas.activity.LiveActivity;
  10. import com.qingdaofushan.homecaresas.event.EventData;
  11. import com.qingdaofushan.homecaresas.utils.SharedPreferenceUtil;
  12. import com.qingdaofushan.homecaresas.utils.ToastUtil;
  13. import com.qingdaofushan.homecaresas.zxing.activity.CaptureActivity;
  14. import org.greenrobot.eventbus.EventBus;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17. public class AndroidInterface {
  18. private Handler deliver = new Handler(Looper.getMainLooper());
  19. private AgentWeb mAgentWeb;
  20. private Activity activity;
  21. public AndroidInterface(AgentWeb mAgentWeb, Activity activity) {
  22. this.mAgentWeb = mAgentWeb;
  23. this.activity = activity;
  24. }
  25. @JavascriptInterface
  26. public void SetData(String json) {
  27. deliver.post(new Runnable() {
  28. @Override
  29. public void run() {
  30. try {
  31. JSONObject jsonObject=new JSONObject(json);
  32. SharedPreferenceUtil.put(activity,jsonObject.getString("key"),jsonObject.getString("value"));
  33. // mAgentWeb.getJsAccessEntrace().quickCallJs("SetData","200");
  34. } catch (JSONException e) {
  35. ToastUtil.showToast("存储参数报错");
  36. }
  37. }
  38. });
  39. }
  40. @JavascriptInterface
  41. public void GetData(String json) {
  42. deliver.post(new Runnable() {
  43. @Override
  44. public void run() {
  45. try {
  46. JSONObject jsonObject=new JSONObject(json);
  47. String key= jsonObject.getString("key");
  48. mAgentWeb.getJsAccessEntrace().quickCallJs("GetData", (String)SharedPreferenceUtil.get(activity,key,""));
  49. } catch (JSONException e) {
  50. ToastUtil.showToast("读取参数报错");
  51. }
  52. }
  53. });
  54. }
  55. @JavascriptInterface
  56. public void QRCode(String a) {
  57. deliver.post(new Runnable() {
  58. @Override
  59. public void run() {
  60. //跳转的默认扫码界面
  61. Intent intent = new Intent(activity, CaptureActivity.class);
  62. activity.startActivityForResult(intent, 200);
  63. // activity.startActivityForResult(new Intent(activity, CaptureActivity.class),200);
  64. }
  65. });
  66. }
  67. @JavascriptInterface
  68. public void StartService(String json) {
  69. deliver.post(new Runnable() {
  70. @Override
  71. public void run() {
  72. try {
  73. JSONObject jsonObject = new JSONObject(json);
  74. String url = jsonObject.getString("url");
  75. Intent intent = new Intent(activity, LiveActivity.class);
  76. intent.putExtra("url", url);
  77. activity.startActivity(intent);
  78. } catch (JSONException e) {
  79. ToastUtil.showToast("读取参数报错");
  80. }
  81. }
  82. });
  83. }
  84. @JavascriptInterface
  85. public void Camera(String a) {
  86. deliver.post(new Runnable() {
  87. @Override
  88. public void run() {
  89. //跳转的拍照
  90. EventBus.getDefault().post(new EventData(3));
  91. }
  92. });
  93. }
  94. }