«

android webview 嵌入html5 定位 文件上传

时间:2024-3-2 19:30     作者:韩俊     分类: Android


什么也不说,直接上代码


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<Uri> 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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xz.testwebview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

    <uses-permission android:name="andorid.permission.CHANGE_CONFIGURATION"/>
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.CAMERA"/> 

   <uses-permission android:name="android.permission.FLASHLIGHT"/>
   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission android:name="android.permission.READ_PHONE_STATE" />
   <uses-permission android:name="android.permission.READ_CONTACTS"/>  
   <uses-permission android:name="android.permission.VIBRATE"/>
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
   <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <!-- 权限 :  GPS定位     -->
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

    <!-- 权限 : AGPS定位      -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>s

    <!-- 文件系统权限  -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.xz.testwebview.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <WebView 
        android:id="@+id/webview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"  

        />

</RelativeLayout>





        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>

标签: android

热门推荐