欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

android webview 嵌入html5 定位 文件上传

来源: 开发者 投稿于  被查看 21120 次 评论:21

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;

	@Override
	protected 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设置336454
		WebSettings webSettings = myWebView.getSettings();

		// 允许使用javascript
		webSettings.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就会返回退出
	 */
	@Override
	public 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

 

 

用户评论