Android Open Source - BarcodeTest Webview Activity






From Project

Back to project page BarcodeTest.

License

The source code is released under:

Copyright (c) 2014, zhongwcool All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * R...

If you think the Android project BarcodeTest listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.qr.activity;
/*from ww  w . j av  a 2s . com*/
import com.ericssonlabs.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

@SuppressLint("NewApi")
public class WebviewActivity extends Activity {
  private WebView webview = null;
  private ProgressBar progressBar = null;
  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.layout_webview);
    
    Intent intentUrl = getIntent();
    String appUrl = intentUrl.getStringExtra("appUrl");
    webview = (WebView) findViewById(R.id.app_center_webview);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setProgress(0);
    
    webview.setInitialScale(50);
    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setAllowFileAccess(true);

    webview.setWebViewClient(new WebViewClient() {
      ProgressDialog loading = new ProgressDialog(WebviewActivity.this){

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
          // TODO Auto-generated method stub
          webview.stopLoading();
          return super.onKeyDown(keyCode, event);
        }
        
      };
      private AlertDialog alertDialog = new AlertDialog.Builder(WebviewActivity.this).create();
      
      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        loading.setMessage(getString(R.string.app_url_loading));
        loading.show();
      }

      @Override
      public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if(loading.isShowing()){
          loading.dismiss();
        }
      }

      @Override
      public void onReceivedError(WebView view, int errorCode,
          String description, String failingUrl) {
        // TODO Auto-generated method stub
        super.onReceivedError(view, errorCode, description, failingUrl);
        alertDialog.setTitle("Error");
        alertDialog.setMessage(description);
        alertDialog.show();
      }
    });
    
    webview.setWebChromeClient(new WebChromeClient(){

      @Override
      public void onProgressChanged(WebView view, int newProgress) {
        // TODO Auto-generated method stub
        super.onProgressChanged(view, newProgress);
        
        progressBar.setProgress(newProgress);
      }
      
    });
    
    webview.setDownloadListener(
        new DownloadListener(){

          @Override
          public void onDownloadStart(String url, String useragent,
              String contentDisposition, String mimetype, long contentLength) {
            // TODO Auto-generated method stub
            String  filename = null;
            Uri uri = Uri.parse(url);
            if(Build.VERSION.SDK_INT >= 9){
              DownloadManager dm = (DownloadManager) getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
              Request req = new Request(uri);
              req.setMimeType(mimetype);
              filename = uri.getPath().substring(uri.getPath().lastIndexOf("/") + 1);
              req.setDescription(filename);
              req.setDestinationInExternalFilesDir(getApplicationContext(), "Download", filename);
              req.setTitle(filename);
              req.setAllowedNetworkTypes(Request.NETWORK_WIFI | Request.NETWORK_MOBILE);
              if(Build.VERSION.SDK_INT >= 11){
                req.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
              }else {
                req.setShowRunningNotification(true);
              }
            
              dm.enqueue(req);
            }else {
              Intent intent = new Intent(Intent.ACTION_VIEW, uri);
              startActivity(intent);
            }
          }
          
        }
        );
    
    webview.loadUrl(appUrl);
  }

  @Override
  protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
      webview.goBack();
      return true;
    } else {
      finish();
    }
    return super.onKeyDown(keyCode, event);
  }
}




Java Source Code List

com.qr.activity.MainActivity.java
com.qr.activity.SearchResultActivity.java
com.qr.activity.WebviewActivity.java
com.qr.utils.Parser.java
com.zxing.activity.CaptureActivity.java
com.zxing.camera.AutoFocusCallback.java
com.zxing.camera.CameraConfigurationManager.java
com.zxing.camera.CameraManager.java
com.zxing.camera.FlashlightManager.java
com.zxing.camera.PlanarYUVLuminanceSource.java
com.zxing.camera.PreviewCallback.java
com.zxing.decoding.CaptureActivityHandler.java
com.zxing.decoding.DecodeFormatManager.java
com.zxing.decoding.DecodeHandler.java
com.zxing.decoding.DecodeThread.java
com.zxing.decoding.FinishListener.java
com.zxing.decoding.InactivityTimer.java
com.zxing.decoding.Intents.java
com.zxing.encoding.EncodingHandler.java
com.zxing.view.ResultUrlAdapter.java
com.zxing.view.Utility.java
com.zxing.view.ViewfinderResultPointCallback.java
com.zxing.view.ViewfinderView.java