forked from kmcgill88/admob_flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement interstitial ads on Android
- Loading branch information
1 parent
cf578e1
commit 51bc5a1
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
android/src/main/kotlin/com/shatsy/admobflutter/AdmobInterstitial.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.shatsy.admobflutter | ||
|
||
import android.content.Context | ||
import com.google.android.gms.ads.AdRequest | ||
import com.google.android.gms.ads.InterstitialAd | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
|
||
class AdmobInterstitial(private val context: Context): MethodChannel.MethodCallHandler { | ||
companion object { | ||
val allAds: MutableMap<Int, InterstitialAd> = mutableMapOf() | ||
} | ||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { | ||
when(call.method) { | ||
"load" -> { | ||
val id = call.argument<Int>("id") | ||
val adUnitId = call.argument<String>("adUnitId") | ||
val adRequest = AdRequest.Builder().build() | ||
|
||
allAds[id!!] = InterstitialAd(context) | ||
allAds[id]!!.adUnitId = adUnitId | ||
allAds[id]?.loadAd(adRequest) | ||
result.success(null) | ||
} | ||
"isLoaded" -> { | ||
val id = call.argument<Int>("id") | ||
|
||
if (allAds[id]!!.isLoaded) { | ||
result.success(true) | ||
} else result.success(false) | ||
} | ||
"show" -> { | ||
val id = call.argument<Int>("id") | ||
|
||
if (allAds[id]!!.isLoaded) { | ||
allAds[id]!!.show() | ||
} else result.error(null, null, null) | ||
} | ||
"dispose" -> { | ||
val id = call.argument<Int>("id") | ||
|
||
allAds.remove(id) | ||
} | ||
else -> result.notImplemented() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'package:flutter/services.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
class AdmobInterstitial { | ||
static Map<int, AdmobInterstitial> _allAds = <int, AdmobInterstitial>{}; | ||
static const MethodChannel _channel = | ||
const MethodChannel('admob_flutter/interstitial'); | ||
|
||
int id; | ||
final String adUnitId; | ||
|
||
AdmobInterstitial({@required this.adUnitId}) { | ||
id = hashCode; | ||
_allAds[id] = this; | ||
} | ||
|
||
Future<bool> get isLoaded async { | ||
final bool result = await _channel.invokeMethod('isLoaded', <String, dynamic>{ | ||
'id': id, | ||
}); | ||
return result; | ||
} | ||
|
||
void loadAd() { | ||
_channel.invokeMethod('load', <String, dynamic>{ | ||
'id': id, | ||
'adUnitId': adUnitId, | ||
}); | ||
} | ||
|
||
void show() async { | ||
if (await isLoaded == true) { | ||
_channel.invokeMethod('show', <String, dynamic>{ | ||
'id': id, | ||
}); | ||
} | ||
} | ||
|
||
void dispose() async { | ||
await _channel.invokeMethod('dispose', <String, dynamic>{ | ||
'id': id, | ||
}); | ||
|
||
_allAds.remove(id); | ||
} | ||
} |