赞
踩
private void initWebView() {
webView = findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// if (getMainActivity().isLoadSuccess){//无网络处理临时屏蔽
// imageView.setVisibility(View.GONE);//无网络处理临时屏蔽
// refreshBtn.setVisibility(View.GONE);//无网络处理临时屏蔽
// }//无网络处理临时屏蔽
// 没作用,不管页面是否成功加载,都会调用至此处
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)
{
super.onReceivedError(webView,request,error);
// reload();
// imageView.setVisibility(View.VISIBLE);//无网络处理临时屏蔽
// refreshBtn.setVisibility(View.VISIBLE);//无网络处理临时屏蔽
// getMainActivity().isLoadSuccess = false ;//无网络处理临时屏蔽
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
{
super.onReceivedSslError(view,handler,error);
// reload();
}
});
//屏幕常亮
webView.setKeepScreenOn(true);
// double t1 = Math.random();
long t1 = System.currentTimeMillis();
// double t1 = System.currentTimeMillis();
String url = String.format(URL, t1, IP, PORT);
webView.loadUrl(url);
jsInterface = new JSInterface(this);
platform = new PlatformAndroid(this);
webView.addJavascriptInterface(jsInterface,"android");
// StatisticsManager.initAppLog(MainActivity.this,TouTiaoAppID,"toutiao");
// imageView = findViewById(R.id.abnormalImg) ;//无网络处理临时屏蔽
// refreshBtn = findViewById(R.id.refreshButton) ;//无网络处理临时屏蔽
// refreshBtn.setOnClickListener(new View.OnClickListener() {//无网络处理临时屏蔽
// @Override//无网络处理临时屏蔽
// public void onClick(View v) {//无网络处理临时屏蔽
// getMainActivity().isLoadSuccess = true ;//无网络处理临时屏蔽
// reload();//无网络处理临时屏蔽
// }//无网络处理临时屏蔽
// });//无网络处理临时屏蔽
}
/**
* 设置全屏
*/
private void fullScreen() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); //底部导航栏自动隐藏
}
package com.kgjh.xcx.wd;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.webkit.JavascriptInterface;
//import com.gyyx.androidsdk.UAMain;
//import com.reyun.tracking.sdk.Tracking;
//import com.umeng.analytics.MobclickAgent;
import androidx.annotation.RequiresApi;
import com.zhangyou.sdk.ZYSDK;
import com.zhangyou.sdk.api.RoleType;
import java.util.HashMap;
import java.util.Map;
/**
* java与js交互接口
*/
public class JSInterface {
private MainActivity activity;
private BroadcastReceiver mReceiver;
private int curBattery = -1;
private int curWifi = -1;
public JSInterface(MainActivity activity) {
this.activity = activity;
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
int level = intent.getIntExtra("level", 0);
setBattery(level);
} else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
int strength = getStrengthLevels(context);
setWifi(strength);
}
}
};
IntentFilter iFilter = new IntentFilter();
iFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
iFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
activity.registerReceiver(mReceiver, iFilter);
}
/************************************ TTS内容 **********************************************/
/**
* 当前语言
* @return
*/
@JavascriptInterface
public String getCurrentLanguage() {
return "gandecheng11111";
}
/**
* 游戏初始化完成
*/
@JavascriptInterface
public void gameInited() {
activity.setGameInited(true);
this.informPackageName();
System.out.println("游戏初始化完成,可以开始平台登陆");
// UAMain.showLogin();
activity.getPlatform().startLogin();
}
/**
* 设置电池电量
* @param count
*/
public void setBattery(int count) {
if (this.curBattery == count) return;
this.curBattery = count;
if (!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:setBattery(" + this.curBattery + ")");
}
/**
* 获取电池电量
* @return
*/
@JavascriptInterface
public int getBattery() {
return this.curBattery;
}
public int getStrengthLevels(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
if (info.getBSSID() != null) {
int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5);
return strength;
}
return 0;
}
/**
* 设置wifi强度
* @param value
*/
public void setWifi(int value) {
if (this.curWifi == value) return;
this.curWifi = value;
if (!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:setWifi(" + this.curWifi + ")");
}
/**
* 获取wifi强度
* @return
*/
@JavascriptInterface
public int getWifi() {
return this.curWifi;
// WifiManager wifiManager = (WifiManager)activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
// int level = wifiInfo.getRssi();
// return level;
}
/**
* 获取设备唯一识别码
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.O)
@JavascriptInterface
public String getIMEI() {
return DeviceIdUtil.getDeviceId(activity);
}
/**
* 获取手机型号
* @return
*/
@JavascriptInterface
public String getSystemModel()
{
return SystemUtil.getSystemModel();
}
/**
* 获取系统版本号
* @return
*/
@JavascriptInterface
public String getSystemVersion()
{
return SystemUtil.getSystemVersion();
}
/**
* 平台登陆成功
* @param dataStr 登陆返回数据
*/
public void platformLoginSuccess(String dataStr)
{
if(!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:platformLoginSuccess_zsy('" + dataStr + "')");
}
/**
* 充值成功
*/
public void rechargeSuccess()
{
if(!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:rechargeFinish_nc()");
}
/**
* 切换账号
*/
@JavascriptInterface
public void switchAccount()
{
// System.out.println("收到切换账号请求1");
if(!activity.getGameInited()) return;
// System.out.println("收到切换账号请求2");
activity.getPlatform().switchAccount();
}
/**
* 渠道支付
*
* @param jsonStr 订单信息
*/
@JavascriptInterface
public void pay(String jsonStr)
{
if(!activity.getGameInited()) return;
activity.getPlatform().pay(jsonStr);
}
/**
* 游戏收到充值成功通知原生
* @param data
*/
@JavascriptInterface
public void paySuccess_cn(String data)
{
if(!activity.getGameInited()) return;
String[] list = data.split(",");
if(list.length != 3) return;
Map<String,String> map = new HashMap<>();
map.put("userid",DeviceIdUtil.getDeviceId(activity));
map.put("orderid",list[0]);
map.put("item",list[1]);
map.put("amount",list[2]);
// MobclickAgent.onEvent(activity,"__finish_payment",map);
}
@JavascriptInterface
public void reportData(int key,String value)
{
if(key == 1)
{
//掌游上报 进入游戏
activity.getPlatform().uploadRole(value, RoleType.ENTER_GAME);
}
else if(key == 2)
{
//掌游上报 创建角色
activity.getPlatform().uploadRole(value, RoleType.CREATE_ROLE);
}
else if(key == 3)
{
//掌游上报 升级
activity.getPlatform().uploadRole(value, RoleType.ROLE_LEVEL_UP);
}
}
@JavascriptInterface
public void reportUserData(int key,String value,String guid)
{
String eventName = "" ;
switch (key){
case 1:
eventName = "@role_login" ;//掌游上报 进入游戏
break;
case 2:
eventName = "@role_register" ;//掌游上报 创建角色
break;
case 3:
eventName = "heartbeat" ;//掌游上报 tick
break;
}
if (eventName!=""){
activity.getPlatform().reportUserData(value, eventName,guid);
}
}
@JavascriptInterface
/**
* 获取手机验证码
*/
public void mobileBindCode(String mobile){
ZYSDK.getApi().apiMobileBindCode(mobile);
}
@JavascriptInterface
/**
* 绑定手机
*/
public void mobileBind(String mobile,String code){
ZYSDK.getApi().apiMobileBind(mobile,code);
}
@JavascriptInterface
/**
* 玩家是否绑定了手机
*/
public boolean isBindPhone(){
return ZYSDK.getUser().isMobileBind();
}
@JavascriptInterface
/**
* 打开外链
*/
public void openBrowser(String _url) {
Uri uri = Uri.parse(_url);
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(uri);
activity.startActivity(intent);
}
@JavascriptInterface
/**
* 关闭外链
*/
public void closeBrowser() {
if(!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:closeBrowser()");
}
/**
* 平台手机绑定成功
*/
public void phoneBindSuccess()
{
if(!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:phoneBindFinish_nc()");
}
/**
* 平台手机绑定失败
*/
public void phoneBindFail()
{
if(!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:phoneBindFail_nc()");
}
/**
* 通知游戏包名
**/
public void informPackageName(){
if(!activity.getGameInited()) return;
String _name = SystemUtil.getMetaDataValue(activity,"CERTIFICATION_INFO") ;//PlatformAndroid.getPackageName(activity);//"com.stgj.stgj.ks.tt.lzk";//activity.getClass().getPackage().getName() ;
String _name1 = _name.replace("\n", "\\\\n") ;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.getWebView().loadUrl("javascript:informPackageName('" + _name1 + "')");
}});
}
/**
* 上报订单事件
* @param param
*/
@JavascriptInterface
public void reportOrder(String param)
{
String[] params = param.split(",");
// Tracking.setOrder(params[0],"CNY",Float.parseFloat(params[1]));
}
/**
* 通知客户端支付成功
*/
public void paySuccess(String payType)
{
if(!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:paySuccess_zsy()");
// GameReportHelper.onEventPurchase("","", activity.getPlatform().productId,1,payType,"¥", true, activity.getPlatform().price);
}
/**
* 通知客户端支付失败
*/
public void payFail(String payType)
{
if(!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:payFail_zsy()");
// GameReportHelper.onEventPurchase("","", activity.getPlatform().productId,1,payType,"¥", false, activity.getPlatform().price);
}
/**
* 显示渠道浮标
*/
@JavascriptInterface
public void showPlatformSign()
{
activity.getPlatform().showPlatformSign();
}
/**
* 退出游戏
*/
@JavascriptInterface
public void quitGame()
{
activity.getPlatform().quitGame();
// MobclickAgent.onProfileSignOff();
}
/**
* 播放广告
*
*/
@JavascriptInterface
public void showAd(String mCodeId) {
activity.getTouTiaoAD().loadRewardedAd(mCodeId);
}
/**
* 广告播放成功
*
*/
public void onAdSuc() {
activity.getWebView().loadUrl("javascript:onAdSuc()");
}
/**
* 广告播放错误
*
*/
public void onAdErr() {
activity.getWebView().loadUrl("javascript:onAdError()");
}
/**
* 广告播放失败
*
*/
public void onAdFail() {
activity.getWebView().loadUrl("javascript:onAdFail()");
}
/**
* get方式发送http
* @param url
*/
@JavascriptInterface
public void sendHttp(String url)
{
activity.sendHttp(url);
}
/**
* 游戏账号创建成功
* @param accountId 账号
*/
@JavascriptInterface
public void createGameAccount(String accountId)
{
// Tracking.setRegisterWithAccountID(accountId);
}
/**
* 游戏账号登陆成功
* @param accountId
*/
@JavascriptInterface
public void gameLogin(String accountId)
{
// Tracking.setLoginSuccessBusiness(accountId);
// MobclickAgent.onProfileSignIn(accountId);
}
/**
* 登录失败
*/
public void youLoftLoginFail()
{
if(!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:loginYouLoftFail_nc()");
}
/**
* QQ登录
*
*/
@JavascriptInterface
public void QQLogin() {
activity.getPlatform().QQLogin();
}
/**
* 微信登录
*
*/
@JavascriptInterface
public void weChatLogin() {
activity.getPlatform().weChatLogin();
}
/**
* 抖音登录
*
*/
@JavascriptInterface
public void dyLogin() {
activity.getPlatform().dyLogin();
}
/**
* 抖音登录
*
*/
@JavascriptInterface
public void phoneLogin() {
activity.getPlatform().phoneLogin();
}
/**
* 尝试上次登录
*
*/
@JavascriptInterface
public void loginYouLoft(String name) {
activity.getPlatform().tryLastLogin(name);
}
/**
* 热云
*
*/
@JavascriptInterface
public void trackingSetPayment_cn(String transactionId,String paymentType,String currencyType,float money) {
activity.getPlatform().trackingSetPayment(transactionId,paymentType,currencyType,money);
}
/**
* 头条注册消息
*
*/
@JavascriptInterface
public void loginSuc_cn(String name) {
activity.getPlatform().loginSuc(name);
}
/**
* 上次登录失败
*/
public void lastLoginFail()
{
if(!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:lastLoginFail_nc()");
}
/**
* 暂存上次授权平台
*/
public void setLastLoginPlat(String name)
{
if(!activity.getGameInited()) return;
activity.getWebView().loadUrl("javascript:setLastLoginPlat_nc('"+name+"')");
}
/**
* 游戏账号创建成功
* @param adPlatform 平台
* @param adId 代码位
* @param fill 是否成功
*/
@JavascriptInterface
public void setAdShow(String adPlatform,String adId,String fill)
{
// Tracking.setAdShow(adPlatform,adId,fill);
}
/**
* 游戏账号创建成功
* @param adPlatform 平台
* @param adId 代码位
*/
@JavascriptInterface
public void setAdClick(String adPlatform,String adId)
{
// Tracking.setAdClick(adPlatform,adId);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。