过去几周我一直试图解决我的高CPU利用率问题,我想我已经确定了问题的来源。我从AndroidLauncher类中注释掉了所有广告初始化代码,CPU利用率接近于零或0.0%。使用广告代码时,我的CPU使用率在10%到20%之间飙升,导致我的三星Galaxy S4过热,如果我只是单独离开一段时间。
使用我自定义实现的类(ActionResolver)调用我的libGDX代码会不会有什么问题?我也在想,我的广告调解会与它有什么关系吗?现在我有谷歌AdMob并在谷歌,千禧媒体和mobfox之间进行调解。无论如何,这是我的安卓代码:
我正在制作横幅广告和插页式广告,并使用一些方法隐藏并在我的ActionResolver中通过调用显示它们。我还有应用内结算功能,并且我已从此代码中删除了Google Play服务代码。
公共类AndroidLauncher扩展AndroidApplication实现ActionResolver {
//Constants for phone sized ads (320x50)
private static final int BANNER_AD_WIDTH = 320;
private static final int BANNER_AD_HEIGHT = 50;
//this Activity
private Activity mActivity;
//Views
protected View gameView; //the libGDX gameView
protected AdView bannerView; //the view for the banner
protected InterstitialAd interstitialAd; //interstitial ad
//In-App Billing
private AndroidBilling androidBilling;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//this
mActivity = this;
// Create the layout
RelativeLayout layout = new RelativeLayout(mActivity);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useImmersiveMode = true;
config.useWakelock = true;
// Create the libgdx View
gameView = createGameView(config);
// Create the banner View
bannerView = createAdView();
startAdvertising(bannerView);
// Add the libgdx view
layout.addView(gameView);
layout.addView(bannerView);
//initialize interstitial ad
interstitialAd = new InterstitialAd(mActivity);
interstitialAd.setAdUnitId(AndroidConstants.AD_UNIT_ID_INTERSTITIAL);
//hide ads till game starts
showAds(false);
// Hook it all up
setContentView(layout);
//create the In-App Billing
androidBilling = new AndroidBilling(mActivity);
}
private View createGameView(AndroidApplicationConfiguration config){
//initialize window parameters
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
gameView = initializeForView(new PaperPig(this), config);
return gameView;
}
//creates the view for banner ad in the bottom
private AdView createAdView(){
AdView bannerAd = new AdView(mActivity);
bannerAd.setAdSize(AdSize.BANNER);
bannerAd.setAdUnitId(AndroidConstants.AD_UNIT_ID_BANNER); //Secret
//Calculate the size of the adView based on the ad size. Replace the width and height values if needed.
int layoutWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, BANNER_AD_WIDTH, getResources().getDisplayMetrics());
int layoutHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, BANNER_AD_HEIGHT, getResources().getDisplayMetrics());
//Create the layout parameters using the calculated adView width and height.
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(layoutWidth, layoutHeight);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bannerAd.setLayoutParams(adParams);
return bannerAd;
}
//request for new banner ad
private void startAdvertising(AdView adView) {
AdRequest.Builder adRequest = new AdRequest.Builder();
adView.loadAd(adRequest.build());
}
// This is the callback that posts a message for the handler
@Override
public void showAds(boolean show) {
handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}
private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;
protected Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case SHOW_ADS:
{
bannerView.setVisibility(View.VISIBLE);
break;
}
case HIDE_ADS:
{
startAdvertising(bannerView); //refresh ad
bannerView.setVisibility(View.GONE);
break;
}
}
}
};
@Override
public void showOrLoadInterstital() {
try {
runOnUiThread(new Runnable() {
public void run() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
else {
AdRequest interstitialRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(interstitialRequest);
}
}
});
} catch (Exception e) {}
}
//***IN-APP BILLING METHODS***//
public void inAppBilling(String iabCall){
if(iabCall.equals("removeAds")){
androidBilling.removeAds();
} else if(iabCall.equals("purchase100")){
androidBilling.purchase100Coconuts();
} else if(iabCall.equals("purchase300")){
androidBilling.purchase300Coconuts();
} else if(iabCall.equals("purchase500")){
androidBilling.purchase500Coconuts();
}
}
@Override
public boolean ismAdsRemoved() {
return androidBilling.ismAdsRemoved();
}
@Override
public int getCoconutPoints() {
return androidBilling.getCoconutPoints();
}
@Override
public void setCoconutPoints(int coconutPoints) {
androidBilling.setCoconutPoints(coconutPoints);
}
@Override
public void setmAdsRemoved(boolean mAdsRemoved) {
androidBilling.setmAdsRemoved(mAdsRemoved);
}
@Override
public boolean getSignedInGPGS() {
// TODO Auto-generated method stub
return false;
}
@Override
public void loginGPGS() {
// TODO Auto-generated method stub
}
@Override
public void logoutGPRS() {
// TODO Auto-generated method stub
}
@Override
public void submitScoreGPGS(long score) {
// TODO Auto-generated method stub
}
@Override
public void unlockAchievementGPGS(String achievementId) {
// TODO Auto-generated method stub
}
@Override
public void getLeaderboardGPGS() {
// TODO Auto-generated method stub
}
@Override
public void getAchievementsGPGS() {
// TODO Auto-generated method stub
}
@Override
public void onResume() {
super.onResume();
//Resume Banner
if (bannerView != null) bannerView.resume();
}
@Override
public void onPause() {
super.onPause();
//Pause Banner
if (bannerView != null) bannerView.pause();
}
@Override
public void onDestroy() {
super.onDestroy();
//Destroy Banner
if (bannerView != null) bannerView.destroy();
//Destroy IAB
androidBilling.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
androidBilling.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onBackPressed() {
final Dialog dialog = new Dialog(mActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LinearLayout ll = new LinearLayout(mActivity);
ll.setOrientation(LinearLayout.VERTICAL);
Button b1 = new Button(mActivity);
b1.setText("Quit the Pig?");
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
ll.addView(b1);
dialog.setContentView(ll);
dialog.show();
}
}
如果有人知道或有任何工具可以帮我弄清楚究竟发生了什么,我的游戏就在这里。
https://play.google.com/store/apps/details?id=com.myertha.paperpig.android