android.webkit.WebView.addJavascriptInterface()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 JavaScript  
字(10.3k)|赞(0)|评价(0)|浏览(252)

本文整理了Java中android.webkit.WebView.addJavascriptInterface()方法的一些代码示例,展示了WebView.addJavascriptInterface()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebView.addJavascriptInterface()方法的具体详情如下:
包路径:android.webkit.WebView
类名称:WebView
方法名:addJavascriptInterface

WebView.addJavascriptInterface介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");

代码示例来源:origin: pockethub/PocketHub

/**
 * Create source editor using given web view
 *
 * @param view
 */
public SourceEditor(final WebView view) {
  WebViewClient client = new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      if (URL_PAGE.equals(url)) {
        view.loadUrl(url);
        return false;
      } else {
        UriLauncherActivity.launchUri(view.getContext(), Uri.parse(url));
        return true;
      }
    }
  };
  view.setWebViewClient(client);
  WebSettings settings = view.getSettings();
  settings.setJavaScriptEnabled(true);
  view.addJavascriptInterface(this, "SourceEditor");
  this.view = view;
}

代码示例来源:origin: Justson/AgentWeb

private JsInterfaceHolder addJavaObjectDirect(String k, Object v) {
  LogUtils.i(TAG, "k:" + k + "  v:" + v);
  this.mWebView.addJavascriptInterface(v, k);
  return this;
}

代码示例来源:origin: stackoverflow.com

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  WebView webView = new WebView(this);
  setContentView(webView);
  WebSettings settings = webView.getSettings();
  settings.setJavaScriptEnabled(true);
  webView.addJavascriptInterface(new MyJavascriptInterface(), "MyClient");
}

代码示例来源:origin: stackoverflow.com

WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

代码示例来源:origin: commonsguy/cw-omnibus

@SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"})
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 mgr=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
 light=mgr.getDefaultSensor(Sensor.TYPE_LIGHT);
 wv=(WebView)findViewById(R.id.webkit);
 wv.getSettings().setJavaScriptEnabled(true);
 wv.addJavascriptInterface(jsInterface, "LIGHT_SENSOR");
 wv.loadUrl("file:///android_asset/index.html");
}

代码示例来源:origin: commonsguy/cw-omnibus

@SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"})
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 mgr=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
 light=mgr.getDefaultSensor(Sensor.TYPE_LIGHT);
 wv=(WebView)findViewById(R.id.webkit);
 wv.getSettings().setJavaScriptEnabled(true);
 wv.addJavascriptInterface(jsInterface, "LIGHT_SENSOR");
 wv.loadUrl(URL);
}

代码示例来源:origin: commonsguy/cw-omnibus

@SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"})
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 mgr=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
 light=mgr.getDefaultSensor(Sensor.TYPE_LIGHT);
 wv=(WebView)findViewById(R.id.webkit);
 wv.getSettings().setJavaScriptEnabled(true);
 wv.addJavascriptInterface(jsInterface, "LIGHT_SENSOR");
 wv.loadUrl("file:///android_asset/index.html");
}

代码示例来源:origin: commonsguy/cw-omnibus

@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle state) {
 super.onCreate(state);
 setContentView(R.layout.main);
 browser=(WebView)findViewById(R.id.webkit);
 myLocationManager=
   (LocationManager)getSystemService(Context.LOCATION_SERVICE);
 browser.getSettings().setJavaScriptEnabled(true);
 browser.addJavascriptInterface(new Locater(), "locater");
 browser.loadUrl("file:///android_asset/geoweb2.html");
}

代码示例来源:origin: commonsguy/cw-omnibus

@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle state) {
 super.onCreate(state);
 
 setContentView(R.layout.main);
 browser=(WebView)findViewById(R.id.webkit);
 
 myLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
 
 browser.getSettings().setJavaScriptEnabled(true);
 browser.addJavascriptInterface(new Locater(), "locater");
 browser.loadUrl("file:///android_asset/geoweb1.html");
}

代码示例来源:origin: stackoverflow.com

wv.addJavascriptInterface(JSInterface, "JSInterface");

代码示例来源:origin: commonsguy/cw-omnibus

@SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"})
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 mgr=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
 light=mgr.getDefaultSensor(Sensor.TYPE_LIGHT);
 wv=(WebView)findViewById(R.id.webkit);
 wv.getSettings().setJavaScriptEnabled(true);
 wv.addJavascriptInterface(jsInterface, "LIGHT_SENSOR");
 if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
  wv.setWebViewClient(new WebViewClient() {
   @Override
   public void onPageFinished(WebView view, String url) {
    initPort();
   }
  });
 }
 wv.loadUrl(URL);
}

代码示例来源:origin: stackoverflow.com

final WebView webview = (WebView) findViewById(R.id.browser);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");

代码示例来源:origin: stackoverflow.com

webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app");

代码示例来源:origin: facebook/facebook-android-sdk

/**
 *  Intended to be used as part of a hybrid webapp.
 *  If you call this method, the FB SDK will add a new JavaScript interface into your webview.
 *  If the FB Pixel is used within the webview, and references the app ID of this app,  then it
 *  will detect the presence of this injected JavaScript object and pass Pixel events back to
 *  the FB SDK for logging using the AppEvents framework.
 *
 * @param webView The webview to augment with the additional JavaScript behaviour
 * @param context Used to access the applicationId and the attributionId for non-authenticated
 *                users.
 */
public static void augmentWebView(WebView webView, Context context) {
  String[] parts = Build.VERSION.RELEASE.split("\\.");
  int majorRelease = parts.length > 0 ? Integer.parseInt(parts[0]) : 0;
  int minorRelease = parts.length > 1 ? Integer.parseInt(parts[1]) : 0;
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1 ||
      majorRelease < 4 || (majorRelease == 4 && minorRelease <= 1)) {
    Logger.log(LoggingBehavior.DEVELOPER_ERRORS, TAG,
        "augmentWebView is only available for Android SDK version >= 17 on devices " +
            "running Android >= 4.2");
    return;
  }
  webView.addJavascriptInterface(new FacebookSDKJSInterface(context),
      "fbmq_" + FacebookSdk.getApplicationId());
}

代码示例来源:origin: Justson/AgentWeb

/**
 * 经过大量的测试,按照以下方式才能保证JS脚本100%注入成功:
 * 1、在第一次loadUrl之前注入JS(在addJavascriptInterface里面注入即可,setWebViewClient和setWebChromeClient要在addJavascriptInterface之前执行);
 * 2、在webViewClient.onPageStarted中都注入JS;
 * 3、在webChromeClient.onProgressChanged中都注入JS,并且不能通过自检查(onJsPrompt里面判断)JS是否注入成功来减少注入JS的次数,因为网页中的JS可以同时打开多个url导致无法控制检查的准确性;
 *
 * @deprecated Android 4.2.2及以上版本的 addJavascriptInterface 方法已经解决了安全问题,如果不使用“网页能将JS函数传到Java层”功能,不建议使用该类,毕竟系统的JS注入效率才是最高的;
 */
@Override
@Deprecated
public final void addJavascriptInterface(Object interfaceObj, String interfaceName) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    super.addJavascriptInterface(interfaceObj, interfaceName);
    Log.i(TAG, "注入");
    return;
  } else {
    Log.i(TAG, "use mJsCallJavas:" + interfaceName);
  }
  LogUtils.i(TAG, "addJavascriptInterface:" + interfaceObj + "   interfaceName:" + interfaceName);
  if (mJsCallJavas == null) {
    mJsCallJavas = new HashMap<String, JsCallJava>();
  }
  mJsCallJavas.put(interfaceName, new JsCallJava(interfaceObj, interfaceName));
  injectJavaScript();
  if (LogUtils.isDebug()) {
    Log.d(TAG, "injectJavaScript, addJavascriptInterface.interfaceObj = " + interfaceObj + ", interfaceName = " + interfaceName);
  }
  addJavascriptInterfaceSupport(interfaceObj, interfaceName);
}

代码示例来源:origin: lipangit/JiaoZiVideoPlayer

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  getSupportActionBar().setDisplayShowHomeEnabled(true);
  getSupportActionBar().setDisplayShowTitleEnabled(true);
  getSupportActionBar().setDisplayUseLogoEnabled(false);
  getSupportActionBar().setTitle("WebView");
  getWindow().setBackgroundDrawable(null);
  setContentView(R.layout.activity_webview);
  mWebView = findViewById(R.id.webview);
  mWebView.getSettings().setJavaScriptEnabled(true);
  mWebView.addJavascriptInterface(new JZCallBack(), "jzvd");
  mWebView.loadUrl("file:///android_asset/jzvd.html");
}

代码示例来源:origin: airbnb/AirMapView

@SuppressLint({ "SetJavaScriptEnabled", "AddJavascriptInterface" })
@Override public View onCreateView(
  LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View view = inflater.inflate(R.layout.fragment_webview, container, false);
 webView = (WebView) view.findViewById(R.id.webview);
 mLayout = (ViewGroup) view;
 WebSettings webViewSettings = webView.getSettings();
 webViewSettings.setSupportZoom(true);
 webViewSettings.setBuiltInZoomControls(false);
 webViewSettings.setJavaScriptEnabled(true);
 webViewSettings.setGeolocationEnabled(true);
 webViewSettings.setAllowFileAccess(false);
 webViewSettings.setAllowContentAccess(false);
 webView.setWebChromeClient(new GeoWebChromeClient());
 AirMapType mapType = AirMapType.fromBundle(getArguments());
 webView.loadDataWithBaseURL(mapType.getDomain(), mapType.getMapData(getResources()),
   "text/html", "base64", null);
 webView.addJavascriptInterface(new MapsJavaScriptInterface(), "AirMapView");
 return view;
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldRecordJavascriptInterfaces() {
 String[] names = {"name1", "name2"};
 for (String name : names) {
  Object obj = new Object();
  assertThat(shadowOf(webView).getJavascriptInterface(name)).isNull();
  webView.addJavascriptInterface(obj, name);
  assertThat(shadowOf(webView).getJavascriptInterface(name)).isSameAs(obj);
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldRemoveJavascriptInterfaces() {
 String name = "myJavascriptInterface";
 webView.addJavascriptInterface(new Object(), name);
 assertThat(shadowOf(webView).getJavascriptInterface(name)).isNotNull();
 webView.removeJavascriptInterface(name);
 assertThat(shadowOf(webView).getJavascriptInterface(name)).isNull();
}

相关文章

WebView类方法