直播电商源码如何实现开屏广告
直播电商源码如何实现开屏广告的相关代码
1.1 添加SplashView。
在XML布局文件中添加SplashView。
Xml 代码
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 | <?xml version= "1.0" encoding= "utf-8" ?> <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".SplashActivity" > <!-- 开屏广告Logo区域 --> <RelativeLayout android:id= "@+id/logo_area" android:layout_width= "match_parent" android:layout_height= "100dp" android:layout_alignParentBottom= "true" android:background= "@android:color/white" android:visibility= "visible" > <LinearLayout android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignParentBottom= "true" android:layout_centerHorizontal= "true" android:layout_marginBottom= "40dp" android:orientation= "vertical" > <LinearLayout android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:layout_marginBottom= "6dp" android:gravity= "center" android:orientation= "horizontal" > <ImageView android:layout_width= "28dp" android:layout_height= "28dp" android:background= "@mipmap/ic_launcher" /> <View android:layout_width= "0.5dp" android:layout_height= "18dp" android:layout_marginLeft= "12dp" android:layout_marginRight= "12dp" android:alpha= "0.1" android:background= "@android:color/black" /> <TextView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:alpha= "1" android:text= "@string/owner" android:textColor= "@android:color/black" android:textSize= "16sp" /> </LinearLayout> <TextView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:alpha= "0.5" android:text= "@string/copyright_info" android:textColor= "@android:color/black" android:textSize= "8sp" /> </LinearLayout> </RelativeLayout> <!-- 开屏广告视图 --> <com.huawei.hms.ads.splash.SplashView android:id= "@+id/splash_ad_view" android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_above= "@id/logo" /> </RelativeLayout> |
以下示例代码展示了如何获取SplashView
1 | SplashView splashView = findViewById(R.id.splash_ad_view); |
1.2 修改应用默认启动页面。
开屏广告是在应用主界面显示之前被展示,所以需修改应用默认启动页面。
修改AndroidManifest.xml, 将默认启动的activity修改为SplashActivity,这样即可在应用主界面加载前展示开屏广告。
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 | <?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "com.huawei.hms.ads.sdk" > <application android:allowBackup= "true" android:icon= "@mipmap/ic_launcher" android:label= "@string/app_name" android:roundIcon= "@mipmap/ic_launcher_round" android:supportsRtl= "true" android:theme= "@style/AppTheme" > <activity android:name= ".MainActivity" android:exported= "false" android:screenOrientation= "portrait" > </activity> <activity android:name= ".SplashActivity" android:exported= "true" android:screenOrientation= "portrait" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> ... </application> </manifest> |
创建SplashActivity.java类,用于实现开屏广告获取和展示。
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 | import android.os.Build; import androidx.appcompat.app.AppCompatActivity; public class SplashActivity extends AppCompatActivity { // "testq6zq98hecj"为测试专用的广告位ID, App正式发布时需要改为正式的广告位ID private static final String AD_ID = "testq6zq98hecj" ; private static final int AD_TIMEOUT = 5000; private static final int MSG_AD_TIMEOUT = 1001; /** * 暂停标志位。 * 在开屏广告页面展示时: * 按返回键退出应用时需设置为true,以确保应用主界面不被拉起; * 切换至其他界面时需设置为false,以确保从其他页面回到开屏广告页面时仍然可以正常跳转至应用主界面; */ private boolean hasPaused = false ; // 收到广告展示超时消息时的回调处理 private Handler timeoutHandler = new Handler( new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message msg) { if (SplashActivity. this .hasWindowFocus()) { jump(); } return false ; } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); // 获取并展示开屏广告 loadAd(); } /** * 广告展示完毕时,从广告界面跳转至App主界面 */ private void jump() { if (!hasPaused) { hasPaused = true ; startActivity( new Intent(SplashActivity. this , MainActivity. class )); finish(); } } /** * 按返回键退出应用时需设置为true,以确保应用主界面不被拉起 */ @Override protected void onStop() { // 移除消息队列中等待的超时消息 timeoutHandler.removeMessages(MSG_AD_TIMEOUT); hasPaused = true ; super.onStop(); } /** * 从其他页面回到开屏页面时调用,进入应用主界面 */ @Override protected void onRestart() { super.onRestart(); hasPaused = false ; jump(); } @Override protected void onDestroy() { super.onDestroy(); |
以上就是直播电商源码如何实现开屏广告的相关代码, 更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现