Advertisement
minafaw3

web

Nov 29th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. package com.authorwjf.javascriptinjection;
  2.  
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.webkit.WebChromeClient;
  7. import android.webkit.WebView;
  8. import android.webkit.WebViewClient;
  9. import android.widget.EditText;
  10. import android.annotation.SuppressLint;
  11. import android.app.Activity;
  12.  
  13. public class MainActivity extends Activity implements OnClickListener {
  14.  
  15. private static final String URL = "file:///android_asset/index.html";
  16. private WebView mWebView;
  17.  
  18. @SuppressLint("SetJavaScriptEnabled")
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. mWebView = (WebView) findViewById(R.id.webview);
  24. mWebView.getSettings().setJavaScriptEnabled(true);
  25. mWebView.setWebChromeClient(new WebChromeClient());
  26. mWebView.setWebViewClient(new WebViewClient(){
  27. @Override
  28. public void onPageFinished(WebView view, String url) {
  29. String user = ((EditText) findViewById(R.id.edit_text)).getText().toString();
  30. if (user.isEmpty()) {
  31. user = "World";
  32. }
  33. String javascript="javascript: document.getElementById('msg').innerHTML='Hello "+user+"!';";
  34. view.loadUrl(javascript);
  35. }
  36. });
  37. refreshWebView();
  38. findViewById(R.id.button).setOnClickListener(this);
  39. }
  40.  
  41. private void refreshWebView() {
  42. mWebView.loadUrl(URL);
  43. }
  44.  
  45. @Override
  46. public void onClick(View v) {
  47. refreshWebView();
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement