Added reload() function for AdMob banner

This commit is contained in:
FalsinSoft 2019-08-06 09:11:41 +02:00
parent 2465a59717
commit 46c9cb6ddd
4 changed files with 38 additions and 10 deletions

View File

@ -277,9 +277,10 @@ onClicked</pre>
ERROR_NETWORK
ERROR_INVALID_REQUEST
ERROR_NO_FILL</pre>
<p>When you want to load and show the banner you have to call the <i>show()</i> function and for hide the <i>hide()</i> functions as follow (banner is the id name of the item in the example above):</p>
<p>When you want to load and show the banner you have to call the <i>show()</i> function and for hide the <i>hide()</i> functions (banner is the id name of the item in the example above). Function <i>reload()</i> force reload of a new banner.</p>
<pre class="prettyprint">banner.show()
banner.hide()</pre>
banner.hide()
banner.reload()</pre>
<p>Please note the banner is a native android view over the QML window, that's mean will stay over anything painted into in and you can not place nothing over the banner.</p>
<img src="images/admobbanner1.png">
</div>

View File

@ -33,7 +33,8 @@ QAndroidAdMobBanner::QAndroidAdMobBanner(QQuickItem *parent) : QQuickItem(parent
"(Landroid/app/Activity;)V",
QtAndroid::androidActivity().object<jobject>()),
m_InstanceIndex(m_InstancesCounter++),
m_BannerType(TYPE_NO_BANNER)
m_BannerType(TYPE_NO_BANNER),
m_BannerShowed(false)
{
m_pInstancesMap[m_InstanceIndex] = this;
@ -74,6 +75,7 @@ bool QAndroidAdMobBanner::show()
{
ItemPosChanged();
m_JavaAdMobBanner.callMethod<void>("show");
m_BannerShowed = true;
return true;
}
@ -85,6 +87,25 @@ bool QAndroidAdMobBanner::hide()
if(m_JavaAdMobBanner.isValid())
{
m_JavaAdMobBanner.callMethod<void>("hide");
m_BannerShowed = false;
return true;
}
return false;
}
bool QAndroidAdMobBanner::reload()
{
if(m_JavaAdMobBanner.isValid() && m_BannerType != TYPE_NO_BANNER && m_UnitId.isEmpty() == false)
{
const bool BannerShowed = m_BannerShowed;
if(BannerShowed) hide();
m_JavaAdMobBanner.callMethod<void>("reload");
setType(m_BannerType);
setUnitId(m_UnitId);
if(BannerShowed) show();
return true;
}
@ -138,13 +159,9 @@ void QAndroidAdMobBanner::ScreenGeometryChanged(const QRect &Geometry)
{
Q_UNUSED(Geometry)
if(m_JavaAdMobBanner.isValid() && m_BannerType != TYPE_NO_BANNER && m_UnitId.isEmpty() == false)
if(m_BannerShowed == true)
{
hide();
m_JavaAdMobBanner.callMethod<void>("reload");
setType(m_BannerType);
setUnitId(m_UnitId);
show();
reload();
}
}

View File

@ -59,6 +59,7 @@ public:
Q_INVOKABLE bool show();
Q_INVOKABLE bool hide();
Q_INVOKABLE bool reload();
const QString& getUnitId() const;
void setUnitId(const QString &UnitId);
@ -85,6 +86,7 @@ private:
static int m_InstancesCounter;
const int m_InstanceIndex;
BANNER_TYPE m_BannerType;
bool m_BannerShowed;
QString m_UnitId;
enum EVENT_TYPE

View File

@ -10,7 +10,7 @@ Page {
Column {
width: parent.wdith
height: parent.height * 0.8
height: parent.height * 0.9
anchors.centerIn: parent
spacing: 20
@ -88,5 +88,13 @@ Page {
banner2.hide();
}
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Reload banners"
onClicked: {
banner1.reload();
banner2.reload();
}
}
}
}