2017-01-08 22 views
-1

私の新しいゲームでは、インタースティシャル広告を表示したいと考えています。ボタンをクリックするとゲームが再開し、15日に1回だけ広告が表示されます。問題は、ゲームが始まるたびに表示されることです。不具合はどこですか?アンドロイド - クリックするとインタースティシャル広告が表示される

public class MainActivity extends AppCompatActivity { 
  
    InterstitialAd mInterstitialAd; 
    public int playcount = 0; 
  
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game); 
     mInterstitialAd = new InterstitialAd(this); 

     // set the ad unit ID 
     mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen)); 

     AdRequest adRequest = new AdRequest.Builder() 
      .build(); 

     // Load ads into Interstitial Ads 
     mInterstitialAd.loadAd(adRequest); 
     TextView playctv = (TextView) findViewById(R.id.textView); 

     SharedPreferences prefsplay = this.getSharedPreferences("myPrefsKey", 
      Context.MODE_PRIVATE); 
     playcount = prefsplay.getInt("play_number", 0); 
     playctv.setText(String.valueOf(playcount)); 
    } 

    public void restart(View view) { 
     Intent intent = new Intent(this, GameScreen.class); 
     startActivity(intent); 
     playcount++; 

     SharedPreferences prefsplay = this 
      .getSharedPreferences("myPrefsKey", 
        Context.MODE_PRIVATE); 
     prefsplay.edit().putInt("play_number", playcount) 
      .apply(); 
    } 

    private void showInterstitial() { 
     if (mInterstitialAd.isLoaded()) 
      mInterstitialAd.show(); 
    } 

    private void adView() { 
     mAdView = (AdView) findViewById(R.id.adView); 
     MobileAds.initialize(getApplicationContext(), getResources().getString(R.string.interstitial_full_screen)); 
     final AdRequest adRequest = new AdRequest.Builder().build(); 
     mAdView.loadAd(adRequest); 
    } 

    public void gameover() { 
     if (playcount == 10) { 
      playcount = 0; 
      mInterstitialAd.setAdListener(new AdListener() { 
       public void onAdLoaded() { 
        mInterstitialAd.show(); //We are executing this code only if ad is loaded, no need to check it again 

        SharedPreferences prefsplay = getApplicationContext() 
         .getSharedPreferences("myPrefsKey", 
           Context.MODE_PRIVATE); 
        prefsplay.edit().putInt("play_number", playcount) 
         .apply(); 

        AdRequest adRequest = new AdRequest.Builder() 
         .build(); 
       } 
      }); 
     } 
    } 
} 

Activity_main:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    android:id="@+id/rect" > 

<ImageView 
    android:id="@+id/imageview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scaleType="center" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/imageview1" 
    android:layout_alignTop="@+id/imageview1" 
    android:alpha="0.7" 
    android:gravity="center" 
    android:text="Score" 
    android:textSize="15sp" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView2" 
    android:alpha="0.9" 
    android:gravity="center" 
    android:text="sco" 
    android:textSize="25sp" /> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/textView1" 
    android:layout_alignBottom="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:text="0:500" 
    android:textSize="25sp" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/textView4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:alpha="0.7" 
    android:layout_alignTop="@+id/textView2" 
    android:text="Highscore" 
    android:textSize="15sp" /> 

<TextView 
    android:id="@+id/textView5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/textView3" 
    android:layout_alignRight="@+id/textView4" 
    android:text="Hs" 
    android:textSize="25sp" /> 



    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     ads:adSize="SMART_BANNER"    ads:adUnitId="@string/banner_home_footer" 
     android:layout_width="wrap_content"  android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true"> 
    </com.google.android.gms.ads.AdView> 

    <ImageButton 
     android:id="@+id/imageButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:drawable/btn_default" 
     android:onClick="restart" 
     android:src="@android:drawable/ic_menu_revert" 

     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView3" 
     android:layout_alignLeft="@+id/imageButton1" 
     android:layout_alignStart="@+id/imageButton1" 
     android:id="@+id/textView" /> 


</RelativeLayout> 

マニフェスト:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="xxx" 
    android:versionCode="2" 
    android:versionName="1.1" > 

    <uses-sdk 
     android:minSdkVersion="14" 
     android:targetSdkVersion="23" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/logoone" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
     <activity 
      android:name=".MainActivity" 
      android:screenOrientation="sensorPortrait" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".GameScreen" 
      android:screenOrientation="sensorPortrait" 
      android:label="@string/app_name" /> 

     <activity 
      android:name="com.google.android.gms.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
      android:theme="@android:style/Theme.Translucent" /> 

    </application> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 


</manifest> 
+0

これは、ポリシーに反していることは、このような事を禁止しています –

+0

本当に?どのくらいそれが現れているかにどのように影響を与えることができますか? –

+0

[https://support.google.com/admob/answer/6201362?hl=en]ここでは何をすべきではないのかを読んでください、それはpragrammatacallyでも可能ですが、彼らは後であなたを怒らせます。 –

答えて

1

あなたのコードは固定の多くを必要とします。私はここにひとつずつ入れています。

  • restart機能を使用すると、結果をGameScreen活動を開始してきたように、あなたは今すぐonActivityResult

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
        switch (requestCode) { 
         case ACTION_GAME: 
          gameover(); 
          break; 
         default: 
          break; 
        } 
    } 
    
  • をオーバーライドする必要がstartActivityForResult

    private final int ACTION_GAME = 100; 
    
    public void restart(View view) { 
        Intent intent = new Intent(this, GameScreen.class); 
        startActivityForResult(intent, ACTION_GAME); 
        playcount++; 
    
        SharedPreferences prefsplay = this 
         .getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
        prefsplay.edit().putInt("play_number", playcount).apply(); 
    } 
    
  • を使用する必要がありますgameover機能を使用するには、保存した値をplaycount。私はこの

    private void loadNewAd() { 
        final AdRequest adRequest = new AdRequest.Builder().build(); 
        mAdView.loadAd(adRequest); 
    } 
    
  • そしてonCreate機能は「あなたと、最後にこの

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_game); 
        mInterstitialAd = new InterstitialAd(this); 
    
        // set the ad unit ID 
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen)); 
    
        // Call adView here 
        adView(); 
    
        TextView playctv = (TextView) findViewById(R.id.textView); 
    
        SharedPreferences prefsplay = this.getSharedPreferences("myPrefsKey", 
         Context.MODE_PRIVATE); 
        playcount = prefsplay.getInt("play_number", 0); 
        playctv.setText(String.valueOf(playcount)); 
    } 
    
  • のように変更する必要があるように見えるかもしれませんloadNewAd()機能を導入しました

    public void gameover() { 
        // Get the saved value 
        SharedPreferences prefsplay = this 
         .getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
        playcount = prefsplay.getInt("play_number", 0); 
    
        if (playcount >= 10) { 
         playcount = 0; 
         showInterstitial(); 
    
         SharedPreferences prefsplay = getApplicationContext() 
          .getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
         prefsplay.edit().putInt("play_number", playcount).apply(); 
    
         // Start loading the new ad 
         loadNewAd(); 
        } 
    } 
    
  • startActivityForResultを使用して、GameScreenのアクティビティを完了している間に結果を設定する必要があります。したがって、アクティビティが終了したか、またはGameScreenアクティビティから戻ると、これを行う必要があります。

    GameScreen.setResult(Activity.RESULT_OK); 
    GameScreen.finish(); 
    
+0

SharedPreferencesは 'prefsplay'を同じスコープで2回使用できないので、私は' gameover() 'メソッドでエラーになります。 –

+0

'SharedPreference'をpublicとして宣言し、' MainActivity'の他のすべての場所でそれを使用します。 –

+0

もちろん、私はします!しかし、あなたが何を意味するのか理解するために、最後の部分は私がGameScreenの仕上げや返却をしている時だけ必要です。だから私はGameScreen自体にい​​る限り、私はそれを必要としないのですか? –

関連する問題