android webview 嵌入html5 定位 檔案上傳

來源:互聯網
上載者:User

android webview 嵌入html5 定位 檔案上傳

什麼也不說,直接上代碼

 

activity

 

package com.xz.testwebview;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.content.res.Configuration;import android.net.Uri;import android.os.Bundle;import android.view.KeyEvent;import android.view.Window;import android.view.WindowManager;import android.webkit.ValueCallback;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebSettings.LayoutAlgorithm;import android.webkit.WebView;import android.webkit.WebViewClient;/** *  * @author 3hxz *  */public class MainActivity extends Activity {private WebView myWebView;private final static int FILECHOOSER_RESULTCODE = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉標題列this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉資訊列setContentView(R.layout.activity_main);// 擷取webview控制項myWebView = (WebView) findViewById(R.id.webview);// 載入伺服器上的頁面myWebView.loadUrl(Contants.url);// 或者載入本地中的html// myWebView.loadUrl(file:///android_asset/www/test2.html);// 加上下面這段代碼可以使網頁中的連結不以瀏覽器的方式開啟myWebView.setWebViewClient(new WebViewClient());// 將WebAppInterface於javascript綁定myWebView.addJavascriptInterface(new WebAppInterface(this), Android);//選擇本地照片檔案myWebView.setWebChromeClient(new WebChromeClient() {// For Android 4.1 (暫時)public void openFileChooser(ValueCallback uploadMsg,String acceptType, String capture) {Intent i = new Intent(Intent.ACTION_GET_CONTENT);i.addCategory(Intent.CATEGORY_OPENABLE);//查看類型 String IMAGE_UNSPECIFIED = image/*;i.setType(image/*);MainActivity.this.startActivityForResult(Intent.createChooser(i, File Chooser),MainActivity.FILECHOOSER_RESULTCODE);} @Override  public void onGeolocationPermissionsHidePrompt() {      super.onGeolocationPermissionsHidePrompt();  }//配置許可權    @Override  public void onGeolocationPermissionsShowPrompt(final String origin,                  final android.webkit.GeolocationPermissions.Callback callback) {      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);      builder.setMessage(是否允許訪問您的地理位置資訊?);      OnClickListener dialogButtonOnClickListener = new OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int clickedButton) {              if (DialogInterface.BUTTON_POSITIVE == clickedButton) {                  callback.invoke(origin, true, true);              } else if (DialogInterface.BUTTON_NEGATIVE == clickedButton) {                  callback.invoke(origin, false, false);              }          }      };      builder.setPositiveButton(允許, dialogButtonOnClickListener);      builder.setNegativeButton(取消, dialogButtonOnClickListener);      builder.show();      super.onGeolocationPermissionsShowPrompt(origin, callback);  }});// 得到webview設定336454WebSettings webSettings = myWebView.getSettings();// 允許使用javascriptwebSettings.setJavaScriptEnabled(true);// 自適應螢幕webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);webSettings.setLoadWithOverviewMode(true);// 設定可以支援縮放webSettings.setSupportZoom(true);// 設定出現縮放工具webSettings.setBuiltInZoomControls(true);// 擴大比例的縮放webSettings.setUseWideViewPort(true);//設定資料庫可用webSettings.setDatabaseEnabled(true);    //擷取地址    String dir = this.getApplicationContext().getDir(database, Context.MODE_PRIVATE).getPath();      webSettings.setDatabasePath(dir);  //使用localStorage則必須開啟          webSettings.setDomStorageEnabled(true);        //啟用地理定位          webSettings.setGeolocationEnabled(true);  }/** * 按鍵響應處理,在WebView中查看網頁時,按返回鍵的時候按瀏覽曆史退回,如果不做此項處理則整個WebView就會返回退出 */@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {// 返回鍵退回myWebView.goBack();return true;}return super.onKeyDown(keyCode, event);}}


 

WebAppInterface

 

 

public class WebAppInterface { Context mContext;        /** Instantiate the interface and set the context */      WebAppInterface(Context c) {          mContext = c;      }        /** Show a toast from the web page */      @JavascriptInterface      public void showToast(String toast) {          Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();      } }

 

 

AndroidManifest.xml

 

                                                                                            s                                                                                   


 

activity_main.xml

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.