移除应用内自定义米盟模块

This commit is contained in:
ZhanGSKen
2025-11-18 16:50:06 +08:00
parent cb81e6e9e4
commit 2db3f2b872
3 changed files with 400 additions and 409 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle #Created by .winboll/winboll_app_build.gradle
#Sun Nov 16 14:00:04 HKT 2025 #Tue Nov 18 08:49:06 GMT 2025
stageCount=2 stageCount=2
libraryProject= libraryProject=
baseVersion=15.11 baseVersion=15.11
publishVersion=15.11.1 publishVersion=15.11.1
buildCount=0 buildCount=4
baseBetaVersion=15.11.2 baseBetaVersion=15.11.2

View File

@@ -60,23 +60,23 @@ public class MainActivity extends WinBoLLActivity {
public static final String TAG = "MainActivity"; public static final String TAG = "MainActivity";
private static final String PRIVACY_FILE = "privacy_pfs"; // private static final String PRIVACY_FILE = "privacy_pfs";
private static final String PRIVACY_VALUE = "privacy_value";//0: 拒绝1赞同 // private static final String PRIVACY_VALUE = "privacy_value";//0: 拒绝1赞同
//
// private SharedPreferences mSharedPreferences;
//
// private String BANNER_POS_ID = "802e356f1726f9ff39c69308bfd6f06a";
// private String BANNER_POS_ID_WINBOLL_BETA = "d129ee5a263911f981a6dc7a9802e3e7";
// private String BANNER_POS_ID_WINBOLL = "4ec30efdb32271765b9a4efac902828b";
private SharedPreferences mSharedPreferences; // private BannerAd mBannerAd;
// private List<BannerAd> mAllBanners = new ArrayList<>();
private String BANNER_POS_ID = "802e356f1726f9ff39c69308bfd6f06a"; //
private String BANNER_POS_ID_WINBOLL_BETA = "d129ee5a263911f981a6dc7a9802e3e7"; // private ViewGroup mContainer;
private String BANNER_POS_ID_WINBOLL = "4ec30efdb32271765b9a4efac902828b"; //
// private boolean mIsBiddingWin = true;
private BannerAd mBannerAd; //
private List<BannerAd> mAllBanners = new ArrayList<>(); // public static final int BACKGROUND_PICTURE_REQUEST_CODE = 0;
private ViewGroup mContainer;
private boolean mIsBiddingWin = true;
public static final int BACKGROUND_PICTURE_REQUEST_CODE = 0;
public static MainActivity _mMainActivity; public static MainActivity _mMainActivity;
private App mApplication; private App mApplication;
@@ -102,13 +102,13 @@ public class MainActivity extends WinBoLLActivity {
LogUtils.d(TAG, "onCreate(...)"); LogUtils.d(TAG, "onCreate(...)");
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
mContainer = findViewById(R.id.ads_container); // mContainer = findViewById(R.id.ads_container);
//
// 初始化主线程Handler关键确保广告操作在主线程执行 // // 初始化主线程Handler关键确保广告操作在主线程执行
mMainHandler = new Handler(Looper.getMainLooper()); // mMainHandler = new Handler(Looper.getMainLooper());
//
// 米盟模块:隐私协议弹窗 // // 米盟模块:隐私协议弹窗
showPrivacy(); // showPrivacy();
_mMainActivity = this; _mMainActivity = this;
mApplication = (App) getApplication(); mApplication = (App) getApplication();
@@ -131,37 +131,37 @@ public class MainActivity extends WinBoLLActivity {
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
// 修复:释放广告资源,避免内存泄漏 // // 修复:释放广告资源,避免内存泄漏
releaseAdResources(); // releaseAdResources();
// 置空静态引用,避免内存泄漏 // 置空静态引用,避免内存泄漏
_mMainActivity = null; _mMainActivity = null;
// 移除Handler回调 // // 移除Handler回调
if (mMainHandler != null) { // if (mMainHandler != null) {
mMainHandler.removeCallbacksAndMessages(null); // mMainHandler.removeCallbacksAndMessages(null);
} // }
}
/**
* 释放广告资源关键避免内存泄漏和空Context调用
*/
private void releaseAdResources() {
LogUtils.d(TAG, "releaseAdResources()");
// 销毁所有广告实例
if (mAllBanners != null && !mAllBanners.isEmpty()) {
for (BannerAd ad : mAllBanners) {
if (ad != null) {
ad.destroy();
}
}
mAllBanners.clear();
}
// 置空当前广告引用
mBannerAd = null;
// 移除广告容器中的视图
if (mContainer != null) {
mContainer.removeAllViews();
}
} }
//
// /**
// * 释放广告资源关键避免内存泄漏和空Context调用
// */
// private void releaseAdResources() {
// LogUtils.d(TAG, "releaseAdResources()");
// // 销毁所有广告实例
// if (mAllBanners != null && !mAllBanners.isEmpty()) {
// for (BannerAd ad : mAllBanners) {
// if (ad != null) {
// ad.destroy();
// }
// }
// mAllBanners.clear();
// }
// // 置空当前广告引用
// mBannerAd = null;
// // 移除广告容器中的视图
// if (mContainer != null) {
// mContainer.removeAllViews();
// }
// }
void showFragment(Fragment fragment) { void showFragment(Fragment fragment) {
FragmentTransaction tx = getFragmentManager().beginTransaction(); FragmentTransaction tx = getFragmentManager().beginTransaction();
@@ -212,23 +212,23 @@ public class MainActivity extends WinBoLLActivity {
reloadBackground(); reloadBackground();
setBackgroundColor(); setBackgroundColor();
// 修复:优化广告请求逻辑(添加生命周期判断 + 主线程执行) // // 修复:优化广告请求逻辑(添加生命周期判断 + 主线程执行)
if (!isFinishing() && !isDestroyed()) { // if (!isFinishing() && !isDestroyed()) {
String privacyAgreeValue = getSharedPreferences().getString(PRIVACY_VALUE, null); // String privacyAgreeValue = getSharedPreferences().getString(PRIVACY_VALUE, null);
if (TextUtils.equals(privacyAgreeValue, String.valueOf(1))) { // if (TextUtils.equals(privacyAgreeValue, String.valueOf(1))) {
LogUtils.i(TAG, "已同意隐私协议,开始播放米盟广告..."); // LogUtils.i(TAG, "已同意隐私协议,开始播放米盟广告...");
mMainHandler.postDelayed(new Runnable() { // mMainHandler.postDelayed(new Runnable() {
@Override // @Override
public void run() { // public void run() {
// 再次校验生命周期避免延迟执行时Activity已销毁 // // 再次校验生命周期避免延迟执行时Activity已销毁
if (!isFinishing() && !isDestroyed()) { // if (!isFinishing() && !isDestroyed()) {
fetchAd(); // fetchAd();
} // }
} // }
}, 1000); // 延迟1秒请求广告提升页面加载体验 // }, 1000); // 延迟1秒请求广告提升页面加载体验
} // }
//
} // }
} }
@Override @Override
@@ -259,15 +259,15 @@ public class MainActivity extends WinBoLLActivity {
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (requestCode == BACKGROUND_PICTURE_REQUEST_CODE) { // if (requestCode == BACKGROUND_PICTURE_REQUEST_CODE) {
if (resultCode == RESULT_OK) { // if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show(); // Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show();
} // }
} else { // } else {
String sz = "Unsolved requestCode = " + Integer.toString(requestCode); // String sz = "Unsolved requestCode = " + Integer.toString(requestCode);
Toast.makeText(getApplicationContext(), sz, Toast.LENGTH_SHORT).show(); // Toast.makeText(getApplicationContext(), sz, Toast.LENGTH_SHORT).show();
LogUtils.d(TAG, sz); // LogUtils.d(TAG, sz);
} // }
} }
@Override @Override
@@ -292,306 +292,306 @@ public class MainActivity extends WinBoLLActivity {
mainLayout.setBackgroundColor(nPixelColor); mainLayout.setBackgroundColor(nPixelColor);
} }
} }
//
/** // /**
* 显示广告核心修复传递安全的Context + 生命周期校验) // * 显示广告核心修复传递安全的Context + 生命周期校验)
*/ // */
private void showAd() { // private void showAd() {
LogUtils.d(TAG, "showAd()"); // LogUtils.d(TAG, "showAd()");
// 1. 生命周期校验避免Activity已销毁时操作UI // // 1. 生命周期校验避免Activity已销毁时操作UI
if (isFinishing() || isDestroyed()) { // if (isFinishing() || isDestroyed()) {
LogUtils.e(TAG, "showAd: Activity is finishing or destroyed"); // LogUtils.e(TAG, "showAd: Activity is finishing or destroyed");
return; // return;
} // }
// 2. 非空校验:广告实例和容器 // // 2. 非空校验:广告实例和容器
if (mBannerAd == null || mContainer == null) { // if (mBannerAd == null || mContainer == null) {
LogUtils.e(TAG, "showAd: BannerAd or Container is null"); // LogUtils.e(TAG, "showAd: BannerAd or Container is null");
return; // return;
} // }
// 3. 创建广告容器使用ApplicationContext避免内存泄漏 // // 3. 创建广告容器使用ApplicationContext避免内存泄漏
final FrameLayout container = new FrameLayout(getApplicationContext()); // final FrameLayout container = new FrameLayout(getApplicationContext());
container.setPadding(0, 0, 0, MimoUtils.dpToPx(this, 10)); // container.setPadding(0, 0, 0, MimoUtils.dpToPx(this, 10));
mContainer.addView(container, new FrameLayout.LayoutParams( // mContainer.addView(container, new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, // FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT // FrameLayout.LayoutParams.WRAP_CONTENT
)); // ));
//
if (mIsBiddingWin) { // if (mIsBiddingWin) {
mBannerAd.setPrice(getPrice()); // mBannerAd.setPrice(getPrice());
} // }
// 4. 显示广告传递ApplicationContext避免Activity Context失效 // // 4. 显示广告传递ApplicationContext避免Activity Context失效
mBannerAd.showAd(MainActivity.this, container, new BannerAd.BannerInteractionListener() { // mBannerAd.showAd(MainActivity.this, container, new BannerAd.BannerInteractionListener() {
@Override // @Override
public void onAdClick() { // public void onAdClick() {
LogUtils.d(TAG, "onAdClick"); // LogUtils.d(TAG, "onAdClick");
} // }
//
@Override // @Override
public void onAdShow() { // public void onAdShow() {
LogUtils.d(TAG, "onAdShow"); // LogUtils.d(TAG, "onAdShow");
} // }
//
@Override // @Override
public void onAdDismiss() { // public void onAdDismiss() {
LogUtils.d(TAG, "onAdDismiss"); // LogUtils.d(TAG, "onAdDismiss");
// 修复移除容器时校验Activity状态 // // 修复移除容器时校验Activity状态
if (!isFinishing() && !isDestroyed() && mContainer != null) { // if (!isFinishing() && !isDestroyed() && mContainer != null) {
mContainer.removeView(container); // mContainer.removeView(container);
} // }
} // }
//
@Override // @Override
public void onRenderSuccess() { // public void onRenderSuccess() {
LogUtils.d(TAG, "onRenderSuccess"); // LogUtils.d(TAG, "onRenderSuccess");
} // }
//
@Override // @Override
public void onRenderFail(int code, String msg) { // public void onRenderFail(int code, String msg) {
LogUtils.e(TAG, "onRenderFail errorCode " + code + " errorMsg " + msg); // LogUtils.e(TAG, "onRenderFail errorCode " + code + " errorMsg " + msg);
// 修复:渲染失败时移除容器 // // 修复:渲染失败时移除容器
if (!isFinishing() && !isDestroyed() && mContainer != null) { // if (!isFinishing() && !isDestroyed() && mContainer != null) {
mContainer.removeView(container); // mContainer.removeView(container);
} // }
} // }
}); // });
} // }
//
/** // /**
* 请求广告核心修复Context安全校验 + 异常捕获 + 资源管理) // * 请求广告核心修复Context安全校验 + 异常捕获 + 资源管理)
*/ // */
private void fetchAd() { // private void fetchAd() {
LogUtils.d(TAG, "fetchAd()"); // LogUtils.d(TAG, "fetchAd()");
// 1. 双重校验Activity未销毁 + Context非空 // // 1. 双重校验Activity未销毁 + Context非空
if (isFinishing() || isDestroyed() || getApplicationContext() == null) { // if (isFinishing() || isDestroyed() || getApplicationContext() == null) {
LogUtils.e(TAG, "fetchAd: Invalid Context or Activity state"); // LogUtils.e(TAG, "fetchAd: Invalid Context or Activity state");
return; // return;
} // }
// 2. 释放之前的广告资源,避免内存泄漏 // // 2. 释放之前的广告资源,避免内存泄漏
if (mBannerAd != null) { // if (mBannerAd != null) {
mBannerAd.destroy(); // mBannerAd.destroy();
} // }
// 3. 初始化广告使用ApplicationContext避免Activity Context失效 // // 3. 初始化广告使用ApplicationContext避免Activity Context失效
try { // try {
mBannerAd = new BannerAd(); // mBannerAd = new BannerAd();
mAllBanners.add(mBannerAd); // mAllBanners.add(mBannerAd);
} catch (Exception e) { // } catch (Exception e) {
LogUtils.e(TAG, "fetchAd: Init BannerAd failed", e); // LogUtils.e(TAG, "fetchAd: Init BannerAd failed", e);
return; // return;
} // }
// 4. 设置下载监听 // // 4. 设置下载监听
mBannerAd.setDownLoadListener(new BannerAd.BannerDownloadListener() { // mBannerAd.setDownLoadListener(new BannerAd.BannerDownloadListener() {
@Override // @Override
public void onDownloadStarted() { // public void onDownloadStarted() {
LogUtils.d(TAG, "onDownloadStarted"); // LogUtils.d(TAG, "onDownloadStarted");
} // }
//
@Override // @Override
public void onDownloadPaused() { // public void onDownloadPaused() {
LogUtils.d(TAG, "onDownloadPaused"); // LogUtils.d(TAG, "onDownloadPaused");
} // }
//
@Override // @Override
public void onDownloadFailed(int errorCode) { // public void onDownloadFailed(int errorCode) {
LogUtils.d(TAG, "onDownloadFailed, errorCode = " + errorCode); // LogUtils.d(TAG, "onDownloadFailed, errorCode = " + errorCode);
} // }
//
@Override // @Override
public void onDownloadFinished() { // public void onDownloadFinished() {
LogUtils.d(TAG, "onDownloadFinished"); // LogUtils.d(TAG, "onDownloadFinished");
} // }
//
@Override // @Override
public void onDownloadProgressUpdated(int progress) { // public void onDownloadProgressUpdated(int progress) {
LogUtils.d(TAG, "onDownloadProgressUpdated " + progress + "%"); // LogUtils.d(TAG, "onDownloadProgressUpdated " + progress + "%");
} // }
//
@Override // @Override
public void onInstallFailed(int errorCode) { // public void onInstallFailed(int errorCode) {
LogUtils.d(TAG, "onInstallFailed, errorCode = " + errorCode); // LogUtils.d(TAG, "onInstallFailed, errorCode = " + errorCode);
} // }
//
@Override // @Override
public void onInstallStart() { // public void onInstallStart() {
LogUtils.d(TAG, "onInstallStart"); // LogUtils.d(TAG, "onInstallStart");
} // }
//
@Override // @Override
public void onInstallSuccess() { // public void onInstallSuccess() {
LogUtils.d(TAG, "onInstallSuccess"); // LogUtils.d(TAG, "onInstallSuccess");
} // }
//
@Override // @Override
public void onDownloadCancel() { // public void onDownloadCancel() {
LogUtils.d(TAG, "onDownloadCancel"); // LogUtils.d(TAG, "onDownloadCancel");
} // }
}); // });
//
// 5. 构建广告参数并请求 // // 5. 构建广告参数并请求
String currentAD_ID = getAD_ID(); // String currentAD_ID = getAD_ID();
LogUtils.d(TAG, String.format("currentAD_ID = %s", currentAD_ID)); // LogUtils.d(TAG, String.format("currentAD_ID = %s", currentAD_ID));
ADParams params = new ADParams.Builder().setUpId(currentAD_ID).build(); // ADParams params = new ADParams.Builder().setUpId(currentAD_ID).build();
mBannerAd.loadAd(params, new BannerAd.BannerLoadListener() { // mBannerAd.loadAd(params, new BannerAd.BannerLoadListener() {
@Override // @Override
public void onBannerAdLoadSuccess() { // public void onBannerAdLoadSuccess() {
LogUtils.d(TAG, "onBannerAdLoadSuccess()"); // LogUtils.d(TAG, "onBannerAdLoadSuccess()");
// 修复广告加载成功后校验Activity状态 // // 修复广告加载成功后校验Activity状态
if (!isFinishing() && !isDestroyed()) { // if (!isFinishing() && !isDestroyed()) {
showAd(); // showAd();
} // }
} // }
//
@Override // @Override
public void onAdLoadFailed(int errorCode, String errorMsg) { // public void onAdLoadFailed(int errorCode, String errorMsg) {
LogUtils.e(TAG, "onAdLoadFailed: errorCode = " + errorCode + ", errorMsg = " + errorMsg); // LogUtils.e(TAG, "onAdLoadFailed: errorCode = " + errorCode + ", errorMsg = " + errorMsg);
// 修复:加载失败时移除当前广告实例 // // 修复:加载失败时移除当前广告实例
if (mAllBanners.contains(mBannerAd)) { // if (mAllBanners.contains(mBannerAd)) {
mAllBanners.remove(mBannerAd); // mAllBanners.remove(mBannerAd);
} // }
mBannerAd.destroy(); // mBannerAd.destroy();
mBannerAd = null; // mBannerAd = null;
} // }
}); // });
} // }
//
/** // /**
* 根据当前秒数获取广告ID原逻辑保留 // * 根据当前秒数获取广告ID原逻辑保留
*/ // */
private String getAD_ID() { // private String getAD_ID() {
long currentSecond = System.currentTimeMillis() / 1000; // long currentSecond = System.currentTimeMillis() / 1000;
return (currentSecond % 2 == 0) ? BANNER_POS_ID : // return (currentSecond % 2 == 0) ? BANNER_POS_ID :
(BuildConfig.DEBUG ? BANNER_POS_ID_WINBOLL_BETA : BANNER_POS_ID_WINBOLL); // (BuildConfig.DEBUG ? BANNER_POS_ID_WINBOLL_BETA : BANNER_POS_ID_WINBOLL);
} // }
//
/** // /**
* 获取广告价格(原逻辑保留,添加空指针校验) // * 获取广告价格(原逻辑保留,添加空指针校验)
*/ // */
private long getPrice() { // private long getPrice() {
if (mBannerAd == null) { // if (mBannerAd == null) {
return 0; // return 0;
} // }
Map<String, Object> map = mBannerAd.getMediaExtraInfo(); // Map<String, Object> map = mBannerAd.getMediaExtraInfo();
if (map == null || map.isEmpty() || !map.containsKey("price")) { // if (map == null || map.isEmpty() || !map.containsKey("price")) {
LogUtils.w(TAG, "getPrice: media extra info is null or no price key"); // LogUtils.w(TAG, "getPrice: media extra info is null or no price key");
return 0; // return 0;
} // }
Object priceObj = map.get("price"); // Object priceObj = map.get("price");
if (priceObj instanceof Long) { // if (priceObj instanceof Long) {
return (Long) priceObj; // return (Long) priceObj;
} else if (priceObj instanceof Integer) { // } else if (priceObj instanceof Integer) {
return ((Integer) priceObj).longValue(); // return ((Integer) priceObj).longValue();
} else { // } else {
LogUtils.e(TAG, "getPrice: price type is invalid"); // LogUtils.e(TAG, "getPrice: price type is invalid");
return 0; // return 0;
} // }
} // }
//
/** // /**
* 显示隐私协议弹窗原逻辑保留优化Context使用 // * 显示隐私协议弹窗原逻辑保留优化Context使用
*/ // */
private void showPrivacy() { // private void showPrivacy() {
// 校验Activity状态避免弹窗泄露 // // 校验Activity状态避免弹窗泄露
if (isFinishing() || isDestroyed()) { // if (isFinishing() || isDestroyed()) {
return; // return;
} // }
String privacyAgreeValue = getSharedPreferences().getString(PRIVACY_VALUE, null); // String privacyAgreeValue = getSharedPreferences().getString(PRIVACY_VALUE, null);
if (TextUtils.equals(privacyAgreeValue, String.valueOf(0))) { // if (TextUtils.equals(privacyAgreeValue, String.valueOf(0))) {
LogUtils.i(TAG, "已拒绝隐私协议,广告已处于不可用状态..."); // LogUtils.i(TAG, "已拒绝隐私协议,广告已处于不可用状态...");
Toast.makeText(getApplicationContext(), "已拒绝隐私协议,广告已处于不可用状态", Toast.LENGTH_SHORT).show(); // Toast.makeText(getApplicationContext(), "已拒绝隐私协议,广告已处于不可用状态", Toast.LENGTH_SHORT).show();
return; // return;
} // }
if (TextUtils.equals(privacyAgreeValue, String.valueOf(1))) { // if (TextUtils.equals(privacyAgreeValue, String.valueOf(1))) {
LogUtils.i(TAG, "已同意隐私协议开始初始化米盟SDK..."); // LogUtils.i(TAG, "已同意隐私协议开始初始化米盟SDK...");
initMimoSdk(); // initMimoSdk();
return; // return;
} // }
LogUtils.i(TAG, "开始弹出隐私协议..."); // LogUtils.i(TAG, "开始弹出隐私协议...");
AlertDialog.Builder builder = new AlertDialog.Builder(this); // AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("用户须知"); // builder.setTitle("用户须知");
builder.setMessage("小米广告SDK隐私政策: https://dev.mi.com/distribute/doc/details?pId=1688, 请复制到浏览器查看"); // builder.setMessage("小米广告SDK隐私政策: https://dev.mi.com/distribute/doc/details?pId=1688, 请复制到浏览器查看");
builder.setIcon(R.drawable.ic_launcher); // builder.setIcon(R.drawable.ic_launcher);
builder.setCancelable(false); // 点击对话框以外的区域不消失 // builder.setCancelable(false); // 点击对话框以外的区域不消失
builder.setPositiveButton("同意", new DialogInterface.OnClickListener() { // builder.setPositiveButton("同意", new DialogInterface.OnClickListener() {
@Override // @Override
public void onClick(DialogInterface dialog, int which) { // public void onClick(DialogInterface dialog, int which) {
getSharedPreferences().edit() // getSharedPreferences().edit()
.putString(PRIVACY_VALUE, String.valueOf(1)) // .putString(PRIVACY_VALUE, String.valueOf(1))
.apply(); // .apply();
initMimoSdk(); // initMimoSdk();
dialog.dismiss(); // dialog.dismiss();
} // }
}); // });
builder.setNegativeButton("拒绝", new DialogInterface.OnClickListener() { // builder.setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
@Override // @Override
public void onClick(DialogInterface dialog, int which) { // public void onClick(DialogInterface dialog, int which) {
getSharedPreferences().edit() // getSharedPreferences().edit()
.putString(PRIVACY_VALUE, String.valueOf(0)) // .putString(PRIVACY_VALUE, String.valueOf(0))
.apply(); // .apply();
dialog.dismiss(); // dialog.dismiss();
} // }
}); // });
AlertDialog dialog = builder.create(); // AlertDialog dialog = builder.create();
//
// 配置弹窗位置(底部全屏) // // 配置弹窗位置(底部全屏)
Window window = dialog.getWindow(); // Window window = dialog.getWindow();
if (window != null) { // if (window != null) {
window.setGravity(Gravity.BOTTOM); // window.setGravity(Gravity.BOTTOM);
WindowManager m = getWindowManager(); // WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay(); // Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p = window.getAttributes(); // WindowManager.LayoutParams p = window.getAttributes();
p.width = d.getWidth(); // p.width = d.getWidth();
window.setAttributes(p); // window.setAttributes(p);
} // }
dialog.show(); // dialog.show();
} // }
//
/** // /**
* 初始化米盟SDK核心修复传递ApplicationContext + 异常捕获) // * 初始化米盟SDK核心修复传递ApplicationContext + 异常捕获)
*/ // */
private void initMimoSdk() { // private void initMimoSdk() {
// 1. 安全获取ApplicationContext避免Activity Context失效 // // 1. 安全获取ApplicationContext避免Activity Context失效
Context appContext = getApplicationContext(); // Context appContext = getApplicationContext();
if (appContext == null) { // if (appContext == null) {
Log.e(TAG, "initMimoSdk: ApplicationContext is null"); // Log.e(TAG, "initMimoSdk: ApplicationContext is null");
return; // return;
} // }
// 2. 初始化SDK捕获异常避免崩溃 // // 2. 初始化SDK捕获异常避免崩溃
try { // try {
MimoSdk.init(appContext, new MimoCustomController() { // MimoSdk.init(appContext, new MimoCustomController() {
@Override // @Override
public boolean isCanUseLocation() { // public boolean isCanUseLocation() {
return true; // return true;
} // }
//
@Override // @Override
public MimoLocation getMimoLocation() { // public MimoLocation getMimoLocation() {
return null; // return null;
} // }
//
@Override // @Override
public boolean isCanUseWifiState() { // public boolean isCanUseWifiState() {
return true; // return true;
} // }
//
@Override // @Override
public boolean alist() { // public boolean alist() {
return true; // return true;
} // }
}, new MimoSdk.InitCallback() { // }, new MimoSdk.InitCallback() {
@Override // @Override
public void success() { // public void success() {
Log.d(TAG, "MimoSdk init success"); // Log.d(TAG, "MimoSdk init success");
} // }
//
@Override // @Override
public void fail(int code, String msg) { // public void fail(int code, String msg) {
Log.e(TAG, "MimoSdk init fail, code=" + code + ",msg=" + msg); // Log.e(TAG, "MimoSdk init fail, code=" + code + ",msg=" + msg);
} // }
}); // });
MimoSdk.setDebugOn(true); // MimoSdk.setDebugOn(true);
} catch (Exception e) { // } catch (Exception e) {
Log.e(TAG, "initMimoSdk: init failed", e); // Log.e(TAG, "initMimoSdk: init failed", e);
} // }
} // }
@Override @Override
public boolean dispatchKeyEvent(KeyEvent event) { public boolean dispatchKeyEvent(KeyEvent event) {
@@ -605,23 +605,23 @@ public class MainActivity extends WinBoLLActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(false);
} }
} }
//
/** // /**
* 获取SharedPreferences实例原逻辑保留添加空指针校验 // * 获取SharedPreferences实例原逻辑保留添加空指针校验
*/ // */
public SharedPreferences getSharedPreferences() { // public SharedPreferences getSharedPreferences() {
if (mSharedPreferences == null) { // if (mSharedPreferences == null) {
// 修复使用ApplicationContext获取SharedPreferences避免Activity Context泄露 // // 修复使用ApplicationContext获取SharedPreferences避免Activity Context泄露
Context appContext = getApplicationContext(); // Context appContext = getApplicationContext();
if (appContext != null) { // if (appContext != null) {
mSharedPreferences = appContext.getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE); // mSharedPreferences = appContext.getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE);
} else { // } else {
Log.e(TAG, "getSharedPreferences: ApplicationContext is null"); // Log.e(TAG, "getSharedPreferences: ApplicationContext is null");
// 降级方案若ApplicationContext为空使用Activity Context仅作兼容 // // 降级方案若ApplicationContext为空使用Activity Context仅作兼容
mSharedPreferences = super.getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE); // mSharedPreferences = super.getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE);
} // }
} // }
return mSharedPreferences; // return mSharedPreferences;
} // }
} }

View File

@@ -29,21 +29,12 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:id="@+id/activitymainFrameLayout1"/> android:id="@+id/activitymainFrameLayout1"/>
</RelativeLayout> </RelativeLayout>
<LinearLayout <TextView
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/activitymainLinearLayout1"> android:text="ADs"/>
<LinearLayout
android:id="@+id/ads_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</LinearLayout>
</LinearLayout> </LinearLayout>