-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMyInterstitials.java
More file actions
75 lines (60 loc) · 2.36 KB
/
MyInterstitials.java
File metadata and controls
75 lines (60 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.blogspot.hu2di.qrart.qart.controller;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import com.blogspot.hu2di.qrart.R;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
/**
* Created by HUNGDH on 1/9/2017.
*/
public class MyInterstitials {
private Context context;
private InterstitialAd mInterstitialAd;
private final long DELTA_TIME = 60000;
private final String LAST_TIME_SHOW_ADS = "lastTimeShowAds";
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
public MyInterstitials(Context context) {
this.context = context;
preferences = context.getSharedPreferences(LAST_TIME_SHOW_ADS, Context.MODE_PRIVATE);
initializeInterstitialAds();
}
private void initializeInterstitialAds() {
// Create the InterstitialAd and set the adUnitId.
mInterstitialAd = new InterstitialAd(context);
// Defined in res/values/strings.xml
mInterstitialAd.setAdUnitId(context.getString(R.string.interstitials_ad_unit_id));
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
}
});
requestNewInterstitial();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
mInterstitialAd.loadAd(adRequest);
}
public void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (mInterstitialAd != null && mInterstitialAd.isLoaded() && (System.currentTimeMillis() - getLastTime()) > DELTA_TIME) {
mInterstitialAd.show();
saveLastTime();
}
}
@SuppressLint("CommitPrefEdits")
private void saveLastTime() {
editor = preferences.edit();
editor.clear();
editor.putLong(LAST_TIME_SHOW_ADS, System.currentTimeMillis());
editor.commit();
}
private long getLastTime() {
return preferences.getLong(LAST_TIME_SHOW_ADS, 1);
}
}