u3d内嵌H5游戏 设置cookie

   Intent intent1 = getIntent();
Log.d("SS", "onCreate: ");
String data = intent1.getStringExtra("cookies");
JSONObject result = null;
try {
result = new JSONObject(data);

String id = result.getString("id");
if (id.equals("289")){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
catch(JSONException e)
{

}

webView = (WebView)findViewById(R.id.webView);


WebSettings webSet = webView.getSettings();
webSet.setJavaScriptEnabled(true);
webSet.setAllowFileAccess(true);
// 自适应屏幕
webSet.setUseWideViewPort(true);
webSet.setLoadWithOverviewMode(true);//设置全屏
webSet.setDomStorageEnabled(true);
//声音
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
}


webSet.setDefaultTextEncodingName("UTF-8");
webSet.setJavaScriptEnabled(true);
webSet.setJavaScriptCanOpenWindowsAutomatically(true);
webSet.setAllowFileAccess(true);
webSet.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSet.setSupportZoom(false);
webSet.setBuiltInZoomControls(false);
webSet.setUseWideViewPort(true);
webSet.setSupportMultipleWindows(true);//支持多标签窗口
webSet.setLoadWithOverviewMode(true);
webSet.setAppCacheEnabled(true);
webSet.setDatabaseEnabled(true);
webSet.setDomStorageEnabled(true);
webSet.setGeolocationEnabled(true);


webSet.setCacheMode(WebSettings.LOAD_DEFAULT);
webSet.setAllowFileAccessFromFileURLs(true);
webSet.setUserAgentString(webSet.getUserAgentString() + "35wd");//3500游戏微包专用
webSet.setSaveFormData(true);


LoadUrl();
webView.loadUrl(gameUrl);
webView.setWebViewClient(new WebViewClient(){
// @Override
// public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// String url = request.getUrl().toString();
// LoadUrl();
// view.loadUrl(url);
// }
// return super.shouldOverrideUrlLoading(view, request);
// }

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

if(url.contains("alipays://platformapi")){//针对阿里网页支付特殊处理
boolean visit = checkAliPayInstalled(H5Activity.this);
if(visit){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
} else if(url.contains("weixin://wap/pay?")) {//针对微信支付特殊处理
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
return true;
}
else {
view.loadUrl(url);

}

// LoadUrl();
// view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
}

//判断是否安装支付宝app
public boolean checkAliPayInstalled(Context context) {

Uri uri = Uri.parse("alipays://platformapi/startApp");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
ComponentName componentName = intent.resolveActivity(context.getPackageManager());
return componentName != null;
}


void LoadUrl() {

Intent intent = getIntent();

String data = intent.getStringExtra("cookies");
Log.e("SSSSS", data);
JSONObject result = null;

try {
result = new JSONObject(data);

gameUrl = result.getString("game_url");
JSONObject scoreObj = (JSONObject) result.get("cookie");//对方平台需要的参数 可以免微信登录
String author = scoreObj.getString("value");
String domain = scoreObj.getString("domain");
String expires = scoreObj.getString("expires");
String path = scoreObj.getString("path");
Log.e("author",author);
Log.e("expires",expires);
Log.e("domain",domain);
Log.e("path",path);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
CookieSyncManager.createInstance(H5Activity.this);
}
CookieManager cookieManager = CookieManager.getInstance();

long l = System.currentTimeMillis() + Long.valueOf(expires);
Date date = new Date(l);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
String format1 = format.format(date);
Log.e("format1",format1);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.setCookie(domain, String.format("author=%s",author));
cookieManager.setCookie(domain, String.format("expires=%s",format1));
cookieManager.setCookie(domain, String.format("domain=%s",domain));
cookieManager.setCookie(domain, String.format("path=%s",path));
CookieSyncManager.getInstance().sync();
} else {
cookieManager.removeSessionCookies(null);
cookieManager.setAcceptCookie(true);
cookieManager.setAcceptThirdPartyCookies(webView, true);
cookieManager.setCookie(domain, String.format("author=%s",author));//重点是这块 多个cookie值 需要一个一个设置 不能一次设置
cookieManager.setCookie(domain, String.format("expires=%s",format1));
cookieManager.setCookie(domain, String.format("domain=%s",domain));
cookieManager.setCookie(domain, String.format("path=%s",path));
cookieManager.flush();
}

String newCookie = cookieManager.getCookie(".3500.com");
Log.e("sdsd", newCookie);
} catch (JSONException e) {
e.printStackTrace();
}
}


@Override
protected void onPause()
{
super.onPause();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
webView.onPause();
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// if (webView.canGoBack()) {
// webView.goBack();
// } else {
// H5Activity.this.finish();
// }
H5Activity.this.finish();
return true;
}

return super.onKeyDown(keyCode, event);
}
protected void onDestroy() {
// 销毁 WebView
if (webView != null) {

webView.destroy();
webView = null;
}
super.onDestroy();
}

@Override
protected void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
webView.onResume();
}
}
posted @ 2019-03-14 17:41  U_探索  阅读(789)  评论(0编辑  收藏  举报