package com.tagmae.tsgame_erwang;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.game.webgame.view.SpUtil;
import com.game.webgame.view.dialogexit;
import com.game.webgame.view.pmutil;
import com.lsjwzh.widget.materialloadingprogressbar.WebviewProgressDialog;
import com.tencent.smtt.export.external.interfaces.JsPromptResult;
import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.jx.jyhd.R;
import com.tsgame.tsgame_niuniu.system.Myapplication;
import com.tsgame.tsgame_niuniu.util.QQIntentShareUtil;
import com.tsgame.tsgame_niuniu.util.apputil;
import com.tsgame.tsgame_niuniu.util.DouYinIntentShareUtil;

import java.util.List;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

public class openwebActivity1 extends AppCompatActivity {

	String[] permissons = new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE };
	Bundle savedInstanceState;
	static SharedPreferences Shared;
	String loadurl = "";
	String data;
	WebView x5webview;
	ProgressDialog m_progressDlg;
	WebviewProgressDialog webdialog;
	private boolean isbackfaiish=true;

	// 添加分享按钮引用和分享工具类
	private ImageView btnShareQQ;
	private ImageView btnShareDouyin;
	private QQIntentShareUtil qqShareUtil;
	private DouYinIntentShareUtil douYinIntentShareUtil;
	// 分享测试图片路径
	private String shareImagePath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		hideNavigationBar();
		/**
		 * 方法一 保持屏幕唤醒
		 */


		setContentView(R.layout.openwebview);
		x5webview = (WebView) findViewById(R.id.x5webview);

		try{
			AndroidBug5497Workaround.assistActivity(this);
		}catch(Exception e){
			e.printStackTrace();
		}

		Myapplication.getInstance().addActivity(openwebActivity1.this);

		/*
		 *
		 *
		 * 方法二
		 */

		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		this.savedInstanceState = savedInstanceState;

		// initdate(save);
		if (!hasPermission(permissons)) {

			PermissionGen.needPermission(openwebActivity1.this, 56, permissons);

		} else {

			initdate(savedInstanceState);
		}
		try{
			getclassname();
		}catch(Exception e){
			e.printStackTrace();
		}

	}

	@PermissionSuccess(requestCode = 56)
	public void filstsucces() {

		initdate(savedInstanceState);
	}

	@PermissionFail(requestCode = 56)
	public void filstfail() {

		if (ActivityCompat.shouldShowRequestPermissionRationale(
				openwebActivity1.this,
				Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
			// 未勾选不再提示

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("提示");
			builder.setMessage("亲,当前应用缺少必要权限。不打开将无法使用");

			// 拒绝, 退出应用
			builder.setNegativeButton("退出",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							finish();

						}
					});

			builder.setPositiveButton("打开",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							PermissionGen.needPermission(openwebActivity1.this,
									11,
									Manifest.permission.WRITE_EXTERNAL_STORAGE);
						}
					});
			builder.show();
			System.out.println("拒绝");
		} else {
			// 勾选不再提示
			if (ContextCompat.checkSelfPermission(this,
					Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {

			}
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		//	Myapplication.getInstance().exit();//退出应用
	}

	// 隐藏虚拟按键
	public void hideNavigationBar()
	{
		// 只在API级别11及以上执行此功能（Android 3.0+）
		if (android.os.Build.VERSION.SDK_INT >= 11) {
			int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
					| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
					| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
					| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
					| View.SYSTEM_UI_FLAG_FULLSCREEN; // hide status bar

			if (android.os.Build.VERSION.SDK_INT >= 19) {
				uiFlags |= 0x00001000;    //SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hide navigation bars - compatibility: building API level is lower thatn 19, use magic number directly for higher API target level
			} else {
				uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
			}
			getWindow().getDecorView().setSystemUiVisibility(uiFlags);
		}
	}
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (hasFocus) {
			hideNavigationBar();
		}
	}

	private void getclassname() {
		ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> runningTasks = manager.getRunningTasks(1);
		RunningTaskInfo cinfo = runningTasks.get(0);
		ComponentName component = cinfo.topActivity;
		System.out.println("current activity is " + component.getClassName());
		apputil.activityname = component.getClassName();

	}
	private void initdate(Bundle savedInstanceState) {
		Intent intent1 = getIntent();
		Uri uri=intent1.getData();
		if(uri!=null){
			String scheme= uri.getScheme();
			if(!pmutil.isnullorEmpty(scheme)){
				finish();
			}
		}
		Shared = SpUtil.getSharePerference(openwebActivity1.this);

		Intent in = getIntent();

		loadurl = in.getStringExtra("url");

		String orientation = in.getStringExtra("orientation");

		System.out.println("orientation="+orientation);
		if(!pmutil.isnullorEmpty(orientation)){
			// orientation = 1;  竖屏
			// orientation = 2; 横屏
			if(orientation.equals("1")){
				//设置为竖屏
				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			}else{
				//默认横屏
			}
		}else{

		}

		String title = in.getStringExtra("title");

		if (!pmutil.isnullorEmpty(title)) {
			RelativeLayout rela = (RelativeLayout) findViewById(R.id.relative1);
			rela.setVisibility(View.VISIBLE);
		}

		TextView heardtitle = (TextView) findViewById(R.id.heardtitle);
		heardtitle.setText(title);
		TextView text = (TextView) findViewById(R.id.backtext);

		text.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		ImageView backimage = (ImageView) findViewById(R.id.backimage);
		backimage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		data = in.getStringExtra("data");

		getinit();

		// 初始化分享按钮和分享工具类
		initShareComponents();
	}

	/**
	 * 初始化分享相关组件
	 */
	private void initShareComponents() {
		// 初始化分享工具类
		qqShareUtil = QQIntentShareUtil.getInstance(this);
		douYinIntentShareUtil = DouYinIntentShareUtil.getInstance(this);

		// 设置分享测试图片路径（使用应用内部存储或外部存储中的图片）
		shareImagePath = getExternalFilesDir(null) + "/share_test.png";

		// 初始化测试图片（如果需要分享测试图片，应该在这里创建一个测试图片文件）
		createTestShareImage();

		// 找到按钮控件
		btnShareQQ = findViewById(R.id.btn_share_qq);
		btnShareDouyin = findViewById(R.id.btn_share_douyin);

		// 设置QQ分享按钮点击事件
		btnShareQQ.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				shareToQQ();
			}
		});

		// 设置抖音分享按钮点击事件
		btnShareDouyin.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				shareToDouYin();
			}
		});
	}

	/**
	 * 创建测试分享图片
	 */
	private void createTestShareImage() {
		try {
			// 创建一个测试图片文件
			Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
			bitmap.eraseColor(android.graphics.Color.BLUE);

			java.io.FileOutputStream out = new java.io.FileOutputStream(shareImagePath);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this, "创建测试分享图片失败", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * QQ分享
	 */
	private void shareToQQ() {
		qqShareUtil.setShareCallback(new QQIntentShareUtil.QQShareCallback() {
			@Override
			public void onSuccess() {
				Toast.makeText(openwebActivity1.this, "QQ分享成功", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onError(int code, String message) {
				Toast.makeText(openwebActivity1.this, "QQ分享失败: " + message, Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onCancel() {
				Toast.makeText(openwebActivity1.this, "QQ分享已取消", Toast.LENGTH_SHORT).show();
			}
		});

		qqShareUtil.shareImageToQQ(this, shareImagePath);
	}

	/**
	 * 抖音分享
	 */
	private void shareToDouYin() {
		douYinIntentShareUtil.setShareCallback(new DouYinIntentShareUtil.DouYinShareCallback() {
			@Override
			public void onSuccess() {
				Toast.makeText(openwebActivity1.this, "抖音分享成功", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onError(int code, String message) {
				Toast.makeText(openwebActivity1.this, "抖音分享失败: " + message, Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onCancel() {
				Toast.makeText(openwebActivity1.this, "抖音分享已取消", Toast.LENGTH_SHORT).show();
			}
		});

		douYinIntentShareUtil.shareImageToDouyin(this, shareImagePath);
	}

	private void getinit() {
		// inithandler();

		m_progressDlg = new ProgressDialog(this);
		m_progressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		// 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
		m_progressDlg.setIndeterminate(false);
		m_progressDlg.setCanceledOnTouchOutside(false);

		Shared = SpUtil.getSharePerference(openwebActivity1.this);

		initwebview();

	}

	@SuppressLint({ "SetJavaScriptEnabled", "ClickableViewAccessibility" })
	private void initwebview() {


		setWebViewClient();
		//x5webview.setWebViewClient(new mwebviewclient());
		x5webview.setWebChromeClient(new mchromeClient());

		initWebViewSettings();
		//setWebViewProperty();
		x5webview.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {

				return true;
			}
		});

		x5webview.loadUrl(loadurl);

		x5webview.addJavascriptInterface(new settings(), "settings");

	}

	protected void setWebViewClient() {
		x5webview.setWebViewClient(new WebViewClient() {

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {

				if (url.startsWith("weixin:") || url.startsWith("alipayqr:")
						|| url.startsWith("alipays:")) {
					try {

						startActivity(new Intent(
								"android.intent.action.VIEW", Uri.parse(url)));
					} catch (ActivityNotFoundException localActivityNotFoundException) {
						Toast.makeText(openwebActivity1.this, "请检查是否安装客户端", Toast.LENGTH_SHORT)
								.show();

					}
					return true;
				}else if (url.startsWith("tel:")) {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
					startActivity(intent);
					return super.shouldOverrideUrlLoading(view, url);
				} else {

					return super.shouldOverrideUrlLoading(view, url);
				}
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);

			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
										String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);

			}

			@TargetApi(23)
			@Override
			public void onReceivedHttpError(WebView view,
											WebResourceRequest request,
											WebResourceResponse errorResponse) {
				super.onReceivedHttpError(view, request, errorResponse);

			}
		});
	}

	// 配置webview
	@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" })
	protected void setWebViewProperty() {
		WebSettings settings = x5webview.getSettings();
		// 支持JavaScript
		settings.setJavaScriptEnabled(true);
		// 支持通过js打开新的窗口
		settings.setJavaScriptCanOpenWindowsAutomatically(true);
		settings.setDomStorageEnabled(true);

		settings.setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);// 不使用缓存，只从网络获取数据
		settings.setAppCacheEnabled(false);// 是否设置缓存

	}
	@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" })
	private void initWebViewSettings() {

		if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {

			x5webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
		}

		WebSettings webSetting = x5webview.getSettings();

		x5webview.requestFocus();
		//x5webview.setWebViewClient(new WebViewClient());

		webSetting.setAllowFileAccess(true);
		webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
		// 设置WebView可触摸放大缩小
		webSetting.setSupportZoom(false);
		webSetting.setBuiltInZoomControls(false);
		// WebView双击变大，再双击后变小，当手动放大后，双击可以恢复到原始大小//设置此属性，可任意比例缩放
		webSetting.setUseWideViewPort(false);
		webSetting.setSupportMultipleWindows(false);
		webSetting.setDatabaseEnabled(true);
		webSetting.setDomStorageEnabled(true);
		webSetting.setJavaScriptEnabled(true);

		if(apputil.isNetworkConnected(this)){
			webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);// cache-control决定是否从网络上取数据。
		}else{
			webSetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// 使用缓存
		}

		webSetting.setAppCacheEnabled(true);// 是否设置缓存
		webSetting.setGeolocationEnabled(true);// //设置定位的数据库路径

	}

	/**
	 * 退出程序
	 */
	private void exitApp() {
		finish();
	}

	class mwebviewclient extends WebViewClient {

		@Override
		public void onPageStarted(WebView arg0, String arg1, Bitmap arg2) {
			// TODO Auto-generated method stub
			super.onPageStarted(arg0, arg1, arg2);
		}

		@Override
		public void onPageFinished(WebView arg0, String arg1) {
			// TODO Auto-generated method stub
			super.onPageFinished(arg0, arg1);
		}

		@Override
		public void onLoadResource(WebView webView, String s) {
			super.onLoadResource(webView, s);
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (url.startsWith("weixin:") || url.startsWith("alipayqr:")
					|| url.startsWith("alipays:")) {
				try {
					//LogUtil.d("启动微信客户端", "-------");
					// hideProgressDialog();
					startActivity(new Intent(
							"android.intent.action.VIEW", Uri.parse(url)));
				} catch (ActivityNotFoundException localActivityNotFoundException) {
					Toast.makeText(openwebActivity1.this, "请检查是否安装客户端", Toast.LENGTH_SHORT)
							.show();

				}
				return true;
			}else if (url.startsWith("tel:")) {
				Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
				startActivity(intent);
				return super.shouldOverrideUrlLoading(view, url);
			} else {
				view.loadUrl(url);
				return super.shouldOverrideUrlLoading(view, url);
			}

		}

		@Override
		public void onReceivedError(WebView webView, int i, String s, String s1) {
			super.onReceivedError(webView, i, s, s1);
			iswebviewerror = true;
		}

		@Override
		public void onReceivedHttpError(WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {
			super.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);
		}

		@Override
		public void onReceivedHttpAuthRequest(
				WebView webview,
				com.tencent.smtt.export.external.interfaces.HttpAuthHandler httpAuthHandlerhost,
				String host, String realm) {

			boolean flag = httpAuthHandlerhost.useHttpAuthUsernamePassword();
		}

	}

	boolean isloddingfinish = false;
	boolean isAamionfinish = false;

	public void webviewjh(final String name) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {

				try {
					x5webview.loadUrl(name);
				} catch (Exception e) {
					System.out.println(e.toString());
				}
			}
		});
	}
	boolean iswebviewerror = false;
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		@SuppressLint({ "NewApi", "InlinedApi" })
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 1:
					int pre = msg.getData().getInt("pre");

					if (hasWindowFocus()) {
						if (webdialog == null) {
							webdialog = WebviewProgressDialog
									.createDialog(openwebActivity1.this);
							webdialog.setCanceledOnTouchOutside(false);

						}

						if (!openwebActivity1.this.isFinishing()
								&& !webdialog.isShowing()) {

							webdialog.show();
						}



						webdialog.setProgress(pre);
					}

					break;

				default:
					break;
			}
		};
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {

			case KeyEvent.KEYCODE_BACK:
				if(x5webview!=null){
					if (x5webview.canGoBack()) {
						if(isbackfaiish){

							finishweb();
						}else{
							String name = "javascript:gamebackkeydown();";
							webviewjh(name);

							x5webview.goBack();
						}

					} else {
						finishweb();

					}
				}else{
					finish();
				}


				return true;

			default:
				break;
		}

		return super.onKeyDown(keyCode, event);
	}

	private boolean isexitdialogeshow=false;

	public void finishweb(){

		if(isexitdialogeshow){
			dialogexit.show(openwebActivity1.this, "是否退出返回游戏",
					new dialogexit.onexitlistener() {
						@Override
						public void paylistener() {
							String name = "javascript:backgameData();";
							if (iswebviewerror) {
								finish();
							} else {
								webviewjh(name);
								handler.postDelayed(new Runnable() {
									@Override
									public void run() {

										if (!openwebActivity1.this.isFinishing()) {
											finish();
										}

									}
								},200);

							}

						}

						@Override
						public void cancellistener() {

						}
					});
		}else{
			finish();
		}
	}
	@Override
	protected void onDestroy() {
		if (webdialog != null) {
			if(webdialog.isShowing()){
				webdialog.dismiss();
			}
			webdialog=null;
		}
		try {
			if (x5webview != null) {
				ViewParent parent = x5webview.getParent();
				if (parent != null) {
					((ViewGroup) parent).removeView(x5webview);
				}

				x5webview.stopLoading();
				x5webview.setWebChromeClient(null);
				x5webview.setWebViewClient(null);
				// 退出时调用此方法，移除绑定的服务，否则某些特定系统会报错
				x5webview.getSettings().setJavaScriptEnabled(false);


				x5webview.clearCache(true);
				x5webview.setFocusable(true); //
				x5webview.removeAllViews();
				x5webview.clearHistory();
				x5webview.clearView();
				x5webview.destroy();
				x5webview = null;

				// android.os.Process.killProcess(android.os.Process.myPid());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}


		super.onDestroy();
	}

	class mchromeClient extends WebChromeClient {

		@Override
		public void onProgressChanged(WebView arg0, int arg1) {

			System.out.println(arg1);
			if (arg1 == 100) {
				//
				if (!isloddingfinish) {
					isloddingfinish = true;

					String getWebdata = "javascript:getWebdata(" + "'" + data
							+ "'" + ");";
					webviewjh(getWebdata);
					System.out.println("getWebdata");
				}

				handler.postDelayed(new Runnable() {
					public void run() {
						if (webdialog != null) {
							if (webdialog.isShowing()) {
								// updates();
								webdialog.dismiss();
							}
						}

					}

				}, 10);

			} else {

				//			    isloddingfinish=false;
				if(!isloddingfinish){
					Message msg = new Message();
					Bundle bundle = new Bundle();
					bundle.putInt("pre", arg1);
					msg.setData(bundle);
					msg.what = 1;
					handler.sendMessage(msg);
				}


			}
			// super.onProgressChanged(arg0, arg1);
		}

		@Override
		public boolean onJsConfirm(WebView arg0, String arg1, String arg2,
								   JsResult arg3) {
			return super.onJsConfirm(arg0, arg1, arg2, arg3);
		}

		/**
		 * webview 的窗口转移
		 */
		@Override
		public boolean onCreateWindow(WebView arg0, boolean arg1, boolean arg2,
									  Message msg) {
			// TODO Auto-generated method stub

			return true;
		}

		@Override
		public boolean onJsAlert(WebView arg0, String arg1, String arg2,
								 JsResult arg3) {
			/**
			 * 这里写入你自定义的window alert
			 */

			Log.i("yuanhaizhou", "setX5webview = null");
			return super.onJsAlert(null, "www.baidu.com", "aa", arg3);
		}

		/**
		 * 对应js 的通知弹框 ，可以用来实现js 和 android之间的通信
		 */
		@Override
		public boolean onJsPrompt(WebView arg0, String arg1, String arg2,
								  String arg3, JsPromptResult arg4) {
			// 在这里可以判定js传过来的数据，用于调起android native 方法

			return super.onJsPrompt(arg0, arg1, arg2, arg3, arg4);
		}

		@Override
		public void onReceivedTitle(WebView arg0, final String arg1) {
			super.onReceivedTitle(arg0, arg1);
			Log.i("yuanhaizhou", "webpage title is " + arg1);

		}

	}

	/**
	 * 为子类提供一个检查权限的方法
	 *
	 * @param permissions
	 * @return
	 */
	public boolean hasPermission(String... permissions) {
		for (String permission : permissions) {
			if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
				return false;

			}
		}
		return true;
	}

	@Override
	public void onRequestPermissionsResult(int requestCode,
										   String[] permissions, int[] grantResults) {

		PermissionGen.onRequestPermissionsResult(this, requestCode,
				permissions, grantResults);
	}
	@SuppressLint("HandlerLeak")
	Handler mhadler1 = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 1:
					try {
						if(!pmutil.isnullorEmpty(url)){

						}
						System.out.println(url);
						x5webview.loadUrl(url);
					} catch (Exception e) {
						System.out.println(e.toString());

					}
					break;
				default:
					break;
			}
		}
	};

	private String url;


	class settings {

		@JavascriptInterface
		public void backgameData(String data) {
			Intent in = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("data", data);
			in.putExtras(bundle);
			setResult(101, in);
			exitApp();
		}
		@JavascriptInterface
		public void browser(String browserurl) {
			Intent intent = new Intent();
			intent.setAction("android.intent.action.VIEW");

			Uri content_url = Uri.parse(browserurl);
			if(BrowserUtil.isQQBrowserInstalled()){
				intent.setClassName("com.tencent.mtt","com.tencent.mtt.MainActivity");//打开QQ浏览器
			}else{
			}
			intent.setData(content_url);
			startActivity(intent);

		}
		@JavascriptInterface
		public void finishweb() {
			finish();
		}

		@JavascriptInterface
		public void isexitdialogeshow() {
			isexitdialogeshow=true;
		}
		@JavascriptInterface
		public void isbackfinishweb() {
			isbackfaiish=false;
		}
		@JavascriptInterface
		public void loadurl(String urls) {

			url = urls;
			System.out.println("url==" + url);
			mhadler1.sendEmptyMessage(1);

		}
	}

}
