MainActivity.java 3.2 KB
package com.qw7277.game;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.qw7277.game.util.AppUtils;

import com.readystatesoftware.systembartint.SystemBarTintManager;

public class MainActivity extends Activity {
    private Context mContext;
    private LinearLayout mLlRoot;
    private WebView mWebView;
    private SystemBarTintManager mTintManager;
    private String mUrl = "https://cdn.kky.cn/yxhz/index.html#/home";
    private String mParams="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mTintManager = new SystemBarTintManager(this);
        mTintManager.setStatusBarTintEnabled(false);
        mTintManager.setNavigationBarTintEnabled(true);
        mTintManager.setStatusBarTintColor(Color.parseColor("#FFFFFF"));

        //适配异形屏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }
        setContentView(R.layout.activity_main);
        initView();
        initWebView();
        initData();
        Log.d("ZhenWan", mUrl+"?"+mParams);
        mWebView.loadUrl(mUrl+"?"+mParams);
    }

    private void initView() {
        mLlRoot = findViewById(R.id.ll_root);
        mLlRoot.setPadding(0, mTintManager.getConfig().getStatusBarHeight(), 0, mTintManager.getConfig().getNavigationBarHeight() + 20);
        mWebView = findViewById(R.id.webview);
    }

    private void initWebView() {
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        webSettings.setAllowFileAccess(true);
    }

    private void initData() {
        Bundle extras = getIntent().getExtras();
        if (extras != null && extras.containsKey("AuthParams")) {
            mParams = extras.getString("AuthParams");
        }
        String cid = AppUtils.getLogicChannel(mContext, "sid");
        mParams += (TextUtils.isEmpty(mParams) ? "" : "&") + "cid=" + cid;
    }

    private long mBackPressedTime = 0;

    @Override
    public void onBackPressed() {
        if (mWebView != null && mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            if (System.currentTimeMillis() - mBackPressedTime > 2000) {
                Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
                mBackPressedTime = System.currentTimeMillis();
            } else {
                super.onBackPressed();
            }
        }
    }
}