WebViewActivity.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package me.yoqi.app.wxredpacket.activities;
  2. import android.annotation.TargetApi;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.view.KeyEvent;
  9. import android.view.View;
  10. import android.view.Window;
  11. import android.view.WindowManager;
  12. import android.webkit.CookieSyncManager;
  13. import android.webkit.WebSettings;
  14. import android.webkit.WebView;
  15. import android.webkit.WebViewClient;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18. import xyz.monkeytong.hongbao.R;
  19. import xyz.monkeytong.hongbao.utils.DownloadUtil;
  20. /**
  21. * Created by Zhongyi on 1/19/16.
  22. * Settings page.
  23. */
  24. public class WebViewActivity extends Activity {
  25. private WebView webView;
  26. private String webViewUrl, webViewTitle;
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. loadUI();
  31. Bundle bundle = getIntent().getExtras();
  32. if (bundle != null && !bundle.isEmpty()) {
  33. webViewTitle = bundle.getString("title");
  34. webViewUrl = bundle.getString("url");
  35. final TextView webViewBar = findViewById(R.id.webview_bar);
  36. webViewBar.setText(webViewTitle);
  37. webView = findViewById(R.id.webView);
  38. webView.getSettings().setBuiltInZoomControls(false);
  39. webView.getSettings().setJavaScriptEnabled(true);
  40. webView.getSettings().setDomStorageEnabled(true);
  41. webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
  42. webView.setWebViewClient(new WebViewClient() {
  43. @Override
  44. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  45. if (url.contains("apk")) {
  46. Toast.makeText(getApplicationContext(), getString(R.string.download_backend), Toast.LENGTH_SHORT).show();
  47. (new DownloadUtil()).enqueue(url, getApplicationContext());
  48. return true;
  49. } else if (!url.contains("http")) {
  50. Toast.makeText(getApplicationContext(), getString(R.string.download_redirect), Toast.LENGTH_LONG).show();
  51. webViewBar.setText(getString(R.string.download_hint));
  52. return false;
  53. } else {
  54. view.loadUrl(url);
  55. return false;
  56. }
  57. }
  58. @Override
  59. public void onPageFinished(WebView view, String url) {
  60. CookieSyncManager.getInstance().sync();
  61. }
  62. });
  63. webView.loadUrl(webViewUrl);
  64. }
  65. }
  66. @Override
  67. protected void onPause() {
  68. super.onPause();
  69. }
  70. @Override
  71. protected void onResume() {
  72. super.onResume();
  73. }
  74. @Override
  75. protected void onDestroy() {
  76. super.onDestroy();
  77. }
  78. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  79. private void loadUI() {
  80. setContentView(R.layout.activity_webview);
  81. if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
  82. Window window = this.getWindow();
  83. window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  84. window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  85. window.setStatusBarColor(0xffE46C62);
  86. }
  87. public void performBack(View view) {
  88. super.onBackPressed();
  89. }
  90. @Override
  91. public boolean onKeyDown(int keyCode, KeyEvent event) {
  92. if (event.getAction() == KeyEvent.ACTION_DOWN) {
  93. switch (keyCode) {
  94. case KeyEvent.KEYCODE_BACK:
  95. if (webView.canGoBack()) {
  96. webView.goBack();
  97. } else {
  98. finish();
  99. }
  100. return true;
  101. }
  102. }
  103. return super.onKeyDown(keyCode, event);
  104. }
  105. public void openLink(View view) {
  106. Intent intent = new Intent(Intent.ACTION_VIEW,
  107. Uri.parse(this.webViewUrl));
  108. startActivity(intent);
  109. }
  110. }