阿赛·游梦鱼·郭言赛·阿赛工作室
技术分类
推荐技术
近期热门技术

安卓APP插入网页WEBVIEW实现全屏,输入法不遮挡等功能

  •  字体:
  • <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eesai.asaiwebview"
    android:windowSoftInputMode="adjustResize">

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".AsaiWeb">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    </manifest>


    package com.eesai.asaiwebview;

    import android.app.ActivityManager;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.webkit.WebChromeClient;
    import android.webkit.WebResourceResponse;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    //manifests/AndroidManifest.xml
    //java/com.eesai.****/AsaiWeb
    //assets/i.html
    //res/layout/asaiweb.xml=WebView.asaiwebview
    public class AsaiWeb extends AppCompatActivity {
    private WebView webView;
    private long exitTime = 0;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
    if (webView.canGoBack()) {
    webView.goBack();//返回上一页面
    return true;
    } else {
    if ((System.currentTimeMillis() - exitTime) > 2000) {
    Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
    exitTime = System.currentTimeMillis();
    return true;
    } else {
    //彻底关闭整个APP
    int currentVersion = android.os.Build.VERSION.SDK_INT;
    if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
    System.exit(0);
    } else {
    ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    am.restartPackage(getPackageName());
    }
    return true;
    }
    }
    }
    return super.onKeyDown(keyCode, event);
    }

    public static boolean isConnect(Context context) {
    boolean _isConnect = false;
    ConnectivityManager conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network = conManager.getActiveNetworkInfo();
    if (network != null) {
    _isConnect = conManager.getActiveNetworkInfo().isAvailable();
    }
    return _isConnect;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
    this.getSupportActionBar().hide();//解决标题栏有时候没法去掉的问题
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉顶部时间等信息栏
    super.onCreate(savedInstanceState);
    setContentView(R.layout.asaiweb);
    this.webView = (WebView) findViewById(R.id.asaiwebview);
    webView.loadData("","text/html","UTF-8");
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);// 让WebView能够执行javaScript
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);// 让JavaScript可以自动打开windows
    webSettings.setDomStorageEnabled(true);//开启 DOM 存储功能
    webSettings.setAppCacheMaxSize(1024 * 1024 * 88);//设置缓冲大小,88M
    webSettings.setDatabaseEnabled(true);//开启 数据库 存储功能
    webSettings.setAppCacheEnabled(true);//开启 应用缓存 功能
    webSettings.setDatabasePath("/asai/cache/muzu");// 设置数据库缓存路径
    webSettings.setAppCachePath("/asai/cache/muzu");//设置 应用 缓存目录
    webSettings.setDefaultTextEncodingName("UTF-8");// 离线文件访问编码
    webSettings.setAppCacheEnabled(true);// 设置缓存
    if (!isConnect(AsaiWeb.this)) {
    webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    } else {
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    }
    // 设置缓存模式,有缓存先读缓存
    // LOAD_DEFAULT: 如果我们应用程序没有设置任何cachemode, 这个是默认的cache方式。 加载一张网页会检查是否有cache,如果有并且没有过期则使用本地cache,否则 从网络上获取。
    // LOAD_CACHE_ELSE_NETWORK: 使用cache资源,即使过期了也使用,如果没有cache才从网络上获取。
    // LOAD_NO_CACHE: 不使用cache 全部从网络上获取
    // LOAD_CACHE_ONLY: 只使用cache上的内容。
    webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);//设置渲染效果优先级,高
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    // 支持内容重新布局,一共有四种方式
    // 默认的是NARROW_COLUMNS
    // 1.NARROW_COLUMNS:可能的话使所有列的宽度不超过屏幕宽度
    // 2.NORMAL:正常显示不做任何渲染
    // 3.SINGLE_COLUMN:把所有内容放大webview等宽的一列中
    webSettings.setSupportZoom(true);// 支持缩放(适配到当前屏幕)
    webSettings.setUseWideViewPort(true);// 将图片调整到合适的大小
    webSettings.setDisplayZoomControls(true);// 设置可以被显示的屏幕控制
    webSettings.setLoadWithOverviewMode(true);
    // webSettings.setDefaultFontSize(12);// 设置默认字体大小
    // 设置WebView的客户端
    webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
    webView.setWebChromeClient(new WebChromeClient());
    webView.loadUrl(url);
    return true;
    }

    //网络请求部分
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

    url = url.toLowerCase();
    if (url.contains("77ya.com") || url.contains("eesai.com")) {
    return super.shouldInterceptRequest(view, url);
    } else {

    //return new WebResourceResponse(null,null,null); //去掉广告
    return null; //去掉广告
    }
    }

    }
    );
    webView.setWebChromeClient(new WebChromeClient());
    if (!isConnect(AsaiWeb.this)) {
    webView.loadUrl("file:///android_asset/i.html");
    } else {
    webView.loadUrl("http://www.77ya.com/");
    }
    AsaiInputMode.getInstance(this).init();
    }
    }


    package com.eesai.asaiwebview;

    import android.app.Activity;
    import android.graphics.Rect;
    import android.view.View;
    import android.view.ViewTreeObserver;
    import android.widget.FrameLayout;


    /**
    * 解决webView键盘遮挡问题的类
    */
    public class AsaiInputMode {
    private Activity activity;
    // private Handler mhanHandler;


    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private static AsaiInputMode keyBoardListener;


    public static AsaiInputMode getInstance(Activity activity) {
    // if(keyBoardListener==null){
    keyBoardListener=new AsaiInputMode(activity);
    // }
    return keyBoardListener;
    }


    public AsaiInputMode(Activity activity) {
    super();
    // TODO Auto-generated constructor stub
    this.activity = activity;
    // this.mhanHandler = handler;

    }


    public void init() {


    FrameLayout content = (FrameLayout) activity
    .findViewById(android.R.id.content);
    mChildOfContent = content.getChildAt(0);
    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout() {
    possiblyResizeChildOfContent();
    }
    });
    frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent
    .getLayoutParams();


    }


    private void possiblyResizeChildOfContent() {
    int usableHeightNow = computeUsableHeight();
    if (usableHeightNow != usableHeightPrevious) {
    int usableHeightSansKeyboard = mChildOfContent.getRootView()
    .getHeight();
    int heightDifference = usableHeightSansKeyboard - usableHeightNow;
    if (heightDifference > (usableHeightSansKeyboard / 4)) {
    // keyboard probably just became visible
    frameLayoutParams.height = usableHeightSansKeyboard
    - heightDifference;
    } else {
    // keyboard probably just became hidden
    frameLayoutParams.height = usableHeightSansKeyboard;
    }
    mChildOfContent.requestLayout();
    usableHeightPrevious = usableHeightNow;
    }
    }


    private int computeUsableHeight() {
    Rect r = new Rect();
    mChildOfContent.getWindowVisibleDisplayFrame(r);
    return (r.bottom - r.top);
    }


    // private void showLog(String title, String msg) {
    // Log.d("Unity", title + "------------>" + msg);
    // }

    }


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <WebView
    android:id="@+id/asaiwebview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    </WebView>

    </LinearLayout>
    Powered by eesai.Com ©  2018  阿赛工作室 All Rights Reserved
    本网站由 阿赛工作室 提供源代码技术支持.  备案号:苏ICP备08114364号

    网站地图建议反馈MapRssXml回到顶部