Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import android.support.v7.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. import android.webkit.WebView;
  4. import android.webkit.WebViewClient;
  5.  
  6. public class MainActivity extends AppCompatActivity {
  7.  
  8. private WebView web;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13.  
  14. web = (WebView) findViewById(R.id.web);
  15. web.setWebViewClient(new WebViewClient());
  16. web.loadUrl("https://site.com");
  17. web.getSettings().setJavaScriptEnabled(true);
  18. }
  19.  
  20. @Override
  21. public void onBackPressed() {
  22. if (web.canGoBack()) {
  23. web.goBack();
  24. } else {
  25. super.onBackPressed();
  26. }
  27. }
  28. }
  29.  
  30. // В этой переменной у нас хранится адрес сайта
  31. String site = "http://site.com/";
  32.  
  33. Uri siteUri = Uri.parse(site);
  34.  
  35. // Создаём интент
  36. Intent openLinkIntent = new Intent(Intent.ACTION_VIEW, siteUri);
  37.  
  38. // Открываем в браузере
  39. startActivity(openLinkIntent);
  40.  
  41. private class MyWebViewClient extends WebViewClient {
  42. @Override
  43. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  44. if (Uri.parse(url).getHost().equals("www.example.com")) {
  45. // This is my web site, so do not override; let my WebView load the page
  46. return false;
  47. }
  48. // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
  49. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  50. startActivity(intent);
  51. return true;
  52. }
  53. }
  54.  
  55. WebView myWebView = (WebView) findViewById(R.id.webview);
  56. myWebView.setWebViewClient(new MyWebViewClient());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement