android - 点击

时间:2017-01-08 00:28:23

标签: java android

在我的新游戏中,我想展示插页式广告。点击按钮点击游戏重启,我希望广告每15次显示一次。问题在于每次游戏开始时都会显示。故障在哪里?

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>

1 个答案:

答案 0 :(得分:1)

您的代码需要大量修复。我一个接一个地放在这里。

  • restart函数应使用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();
    }
    
  • 当您为结果启动GameScreen活动时,您需要覆盖onActivityResult

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ACTION_GAME:
                gameover();
                break;
            default:
                break;
        }
    }
    
  • 现在在gameover功能中,您需要检查playcount首选项中保存的值。

    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();
        }
    }
    
  • 我已经介绍了loadNewAd()函数,可能看起来像这样

    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));
    }
    
  • 最后,当您使用startActivityForResult时,您需要在完成GameScreen活动时设置结果。因此,当您完成GameScreen活动或从GameScreen.setResult(Activity.RESULT_OK); GameScreen.finish(); 活动返回时,您需要执行此操作。

    /^image\/\w+/