Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
A
appzxhy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhouzhi
appzxhy
Commits
c4bbfecc
Commit
c4bbfecc
authored
Jun 03, 2025
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[修改]广告代码修改
parent
30eda931
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
19 deletions
+56
-19
NativeParentView.kt
app/src/main/java/com/base/appzxhy/ads/NativeParentView.kt
+3
-0
AdNativeMgr.kt
app/src/main/java/com/base/appzxhy/ads/admob/AdNativeMgr.kt
+53
-19
No files found.
app/src/main/java/com/base/appzxhy/ads/NativeParentView.kt
View file @
c4bbfecc
...
@@ -23,6 +23,7 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) :
...
@@ -23,6 +23,7 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) :
FrameLayout
(
context
,
attrs
)
{
FrameLayout
(
context
,
attrs
)
{
private
val
TAG
=
"NativeParentView"
private
val
TAG
=
"NativeParentView"
private
var
removeAll
=
true
private
var
removeAll
=
true
var
isAdShowed
=
false
init
{
init
{
// 获取XML属性
// 获取XML属性
...
@@ -78,6 +79,7 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) :
...
@@ -78,6 +79,7 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) :
removeAllViews
()
removeAllViews
()
}
}
addView
(
adView
)
addView
(
adView
)
isAdShowed
=
true
}
}
fun
setNativeAd
(
fun
setNativeAd
(
...
@@ -102,5 +104,6 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) :
...
@@ -102,5 +104,6 @@ class NativeParentView(context: Context, attrs: AttributeSet? = null) :
setBackgroundResource
(
0
)
setBackgroundResource
(
0
)
removeAllViews
()
removeAllViews
()
addView
(
adView
)
addView
(
adView
)
isAdShowed
=
true
}
}
}
}
app/src/main/java/com/base/appzxhy/ads/admob/AdNativeMgr.kt
View file @
c4bbfecc
package
com.base.appzxhy.ads.admob
package
com.base.appzxhy.ads.admob
import
android.content.Context
import
android.util.Log
import
com.base.appzxhy.GlobalConfig
import
com.base.appzxhy.GlobalConfig
import
com.base.appzxhy.MyApplication
import
com.base.appzxhy.ads.AdsType
import
com.base.appzxhy.ads.AdsType
import
com.base.appzxhy.ads.LimitUtils
import
com.base.appzxhy.ads.LimitUtils
import
com.base.appzxhy.ads.NativeParentView
import
com.base.appzxhy.ads.NativeParentView
...
@@ -17,6 +20,8 @@ import java.util.concurrent.ConcurrentLinkedDeque
...
@@ -17,6 +20,8 @@ import java.util.concurrent.ConcurrentLinkedDeque
*/
*/
class
AdNativeMgr
{
class
AdNativeMgr
{
private
val
TAG
=
"AdNativeMgr"
/**
/**
* 上一次的缓存成功时间
* 上一次的缓存成功时间
*/
*/
...
@@ -27,27 +32,37 @@ class AdNativeMgr {
...
@@ -27,27 +32,37 @@ class AdNativeMgr {
*/
*/
private
val
cacheItems
=
ConcurrentLinkedDeque
<
NativeAd
>()
private
val
cacheItems
=
ConcurrentLinkedDeque
<
NativeAd
>()
private
fun
loadAd
(
fun
loadAd
(
context
:
Context
,
admobEvent
:
AdmobEvent
,
admobEvent
:
AdmobEvent
,
parent
:
NativeParentView
,
showAction
:
((
ad
:
NativeAd
)
->
Unit
)?
=
null
layout
:
Int
)
{
)
{
admobEvent
.
adPulStart
()
admobEvent
.
adPulStart
()
if
(!
LimitUtils
.
isAdShow
(
AdsType
.
NATIVE
,
admobEvent
))
return
if
(!
LimitUtils
.
isAdShow
(
AdsType
.
NATIVE
,
admobEvent
))
{
// Log.e(TAG, "loadAd return")
return
}
var
currentNativeAd
:
NativeAd
?
=
null
var
currentNativeAd
:
NativeAd
?
=
null
val
adLoader
=
AdLoader
.
Builder
(
val
adLoader
=
AdLoader
.
Builder
(
parent
.
context
,
context
,
GlobalConfig
.
ID_ADMOB_NATIVE
GlobalConfig
.
ID_ADMOB_NATIVE
).
forNativeAd
{
nativeAd
->
).
forNativeAd
{
nativeAd
->
currentNativeAd
=
nativeAd
cacheItems
.
offer
(
nativeAd
)
lastTime
=
System
.
currentTimeMillis
()
lastTime
=
System
.
currentTimeMillis
()
nativeAd
.
setOnPaidEventListener
(
AdmobEvent
.
EventOnPaidEventListener
(
nativeAd
))
nativeAd
.
setOnPaidEventListener
(
AdmobEvent
.
EventOnPaidEventListener
(
nativeAd
))
currentNativeAd
=
nativeAd
admobEvent
.
pullAd
(
nativeAd
.
responseInfo
)
admobEvent
.
pullAd
(
nativeAd
.
responseInfo
)
show
(
admobEvent
,
parent
,
layout
)
if
(
showAction
!=
null
)
{
showAction
.
invoke
(
nativeAd
)
}
else
{
// Log.e(TAG, "offer ad")
cacheItems
.
offer
(
nativeAd
)
}
}.
withAdListener
(
object
:
AdListener
()
{
}.
withAdListener
(
object
:
AdListener
()
{
...
@@ -64,8 +79,7 @@ class AdNativeMgr {
...
@@ -64,8 +79,7 @@ class AdNativeMgr {
super
.
onAdClosed
()
super
.
onAdClosed
()
}
}
}).
withNativeAdOptions
(
}).
withNativeAdOptions
(
NativeAdOptions
.
Builder
()
NativeAdOptions
.
Builder
().
build
()
.
build
()
).
build
()
).
build
()
adLoader
.
loadAds
(
AdRequest
.
Builder
().
build
(),
1
)
adLoader
.
loadAds
(
AdRequest
.
Builder
().
build
(),
1
)
}
}
...
@@ -80,24 +94,44 @@ class AdNativeMgr {
...
@@ -80,24 +94,44 @@ class AdNativeMgr {
admobEvent
.
adPrepareShow
()
admobEvent
.
adPrepareShow
()
if
(!
LimitUtils
.
isAdShow
(
AdsType
.
NATIVE
,
admobEvent
))
{
if
(!
LimitUtils
.
isAdShow
(
AdsType
.
NATIVE
,
admobEvent
))
{
Log
.
e
(
TAG
,
"!isAdShow"
)
cacheItems
.
clear
()
cacheItems
.
clear
()
return
return
}
}
Log
.
e
(
TAG
,
"adNative can show"
)
val
nativeAd
=
cacheItems
.
peek
()
if
(!
adAvailable
())
{
if
(
nativeAd
==
null
||
!
adAvailable
())
{
//缓存过期了就清空
//缓存过期了就清空
Log
.
e
(
TAG
,
"cacheItems clear"
)
cacheItems
.
clear
()
cacheItems
.
clear
()
loadAd
(
admobEvent
,
parent
,
layout
)
return
}
}
parent
.
isAdShowed
=
true
val
nativeAd
=
cacheItems
.
peek
()
if
(
nativeAd
==
null
)
{
loadAd
(
parent
.
context
,
admobEvent
)
{
ad
->
Log
.
e
(
TAG
,
"load show"
)
nativeCallBack
?.
invoke
(
ad
)
parent
.
setNativeAd
(
ad
,
layout
)
admobEvent
.
showAd
(
ad
.
responseInfo
)
loadAd
(
MyApplication
.
appContext
,
AdmobEvent
(
"nativeAd"
,
"preload"
),
null
)
}
}
else
{
val
flag
=
cacheItems
.
remove
(
nativeAd
)
Log
.
e
(
TAG
,
"ready show remove=$flag size=${cacheItems.size}"
)
nativeCallBack
?.
invoke
(
nativeAd
)
nativeCallBack
?.
invoke
(
nativeAd
)
parent
.
setNativeAd
(
nativeAd
,
layout
)
parent
.
setNativeAd
(
nativeAd
,
layout
)
admobEvent
.
showAd
(
nativeAd
.
responseInfo
)
admobEvent
.
showAd
(
nativeAd
.
responseInfo
)
loadAd
(
MyApplication
.
appContext
,
AdmobEvent
(
"nativeAd"
,
"preload"
),
null
)
}
}
}
private
fun
adAvailable
():
Boolean
{
private
fun
adAvailable
():
Boolean
{
return
((
System
.
currentTimeMillis
()
-
lastTime
)
/
1000
/
60
).
toInt
()
<
30
return
(
lastTime
==
0L
)
||
(
(
System
.
currentTimeMillis
()
-
lastTime
)
/
1000
/
60
).
toInt
()
<
30
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment