您好,登录后才能下订单哦!
Android应用中怎么设计启动页广告?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
Android启动页广告
启动页几乎成为了每个app的标配,有些商家在启动页中增加了开屏广告以此带来更多的收入。
1 activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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="com.landptf.blog.MainActivity"> <fragment android:id="@+id/fm_splash" android:layout_width="match_parent" android:layout_height="match_parent" class="com.landptf.blog.splash.SplashFragment" /> <fragment android:id="@+id/fm_main" android:layout_width="match_parent" android:layout_height="match_parent" class="com.landptf.blog.MainFragment" /> </FrameLayout>
包含了两个fragment,分别是加载广告也和主界面的
2 MainActivity.java
/** * Created by landptf on 2017/03/18. * 主页面,包含了SplashFragment和MainFragment */ public class MainActivity extends AppCompatActivity { private FragmentManager frManager; private SplashFragment fmSplash; private MainFragment fmMain; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { frManager = getSupportFragmentManager(); fmSplash = (SplashFragment) frManager.findFragmentById(R.id.fm_splash); fmMain = (MainFragment) frManager.findFragmentById(R.id.fm_main); showSplash(); } private void showSplash(){ frManager.beginTransaction().hide(fmMain).show(fmSplash).commit(); } public void dismissSplash(){ frManager.beginTransaction().hide(fmSplash).show(fmMain).commitAllowingStateLoss(); } }
在SplashFragment中广告加载完成或者点击跳过后调用dismissSplash将SplashFragment隐藏,将MainFragment显示出来
3 SplashFragment.java
/** * Created by landptf on 2017/03/18. * 启动页,集成了腾讯广告联盟的开屏广告 */ public class SplashFragment extends Fragment { private static final String TAG = SplashFragment.class.getSimpleName(); private MainActivity activity; private ViewGroup container; private TextView tvSkip; private ImageView ivSplashHolder; private static final String SKIP_TEXT = "点击跳过 %d"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_splash, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); activity = (MainActivity) getActivity(); initView(); } private void initView() { container = (ViewGroup) activity.findViewById(R.id.fl_splash_container); tvSkip = (TextView) activity.findViewById(R.id.tv_skip); ivSplashHolder = (ImageView) activity.findViewById(R.id.iv_splash_holder); //申请动态权限 ApplyPermissions(); } /** * 动态申请集成腾讯广告联盟的开屏广告所需要的三个权限 * 使用了RxPermissions开源框架 */ private void ApplyPermissions() { RxPermissions rxPermissions = new RxPermissions(activity); rxPermissions .request(Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE) .subscribe(granted -> { if (granted) { //获取开屏广告 new SplashAD(activity, container, tvSkip, Constants.APPID, Constants.SplashPosID, adListener, 5000); } else { //直接进入主页面 activity.dismissSplash(); } }); } /** * 开屏广告状态的监听 */ private SplashADListener adListener = new SplashADListener() { /** * 广告关闭时调用,可能是用户关闭或者展示时间到。此时一般需要跳过开屏的Activity,进入应用内容页面 */ @Override public void onADDismissed() { activity.dismissSplash(); } /** * 广告加载失败,errCode用于描述失败原因。 * @param i */ @Override public void onNoAD(int i) { Log.e(TAG, "error code = " + i); activity.dismissSplash(); } /** * 广告成功展示时调用 */ @Override public void onADPresent() { ivSplashHolder.setVisibility(View.GONE); } /** * 广告被点击时调用 */ @Override public void onADClicked() { Log.i(TAG, "SplashADClicked"); } /** * 倒计时回调,返回广告还将被展示的剩余时间,单位是ms * @param l */ @Override public void onADTick(long l) { tvSkip.setText(String.format(SKIP_TEXT, Math.round(l / 1000f))); } }; }
这里集成了腾讯广告联盟,点击这里注册,流程比较简单,按照说明一步一步进行就可以了,sdk文档也比较详细。
当广告加载完成后调用 activity.dismissSplash();将其隐藏
4 MainFragment.java
/** * 模拟后台耗时操作 */ private void testThread(){ new Thread(() -> { int i = 0; while (i < 5) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Log.e(TAG, "--- " + i + " ---"); i++; } }).start(); }
在MainFragment主线程中开启了一个只线程来模拟耗时操作,通过log可以看到在广告倒计时的过程中线程已经在执行了。
03-18 03:30:50.348 9491-9513/com.landptf.blog E/MainFragment: --- 0 --- 03-18 03:30:51.348 9491-9513/com.landptf.blog E/MainFragment: --- 1 --- 03-18 03:30:52.348 9491-9513/com.landptf.blog E/MainFragment: --- 2 --- 03-18 03:30:53.349 9491-9513/com.landptf.blog E/MainFragment: --- 3 --- 03-18 03:30:54.350 9491-9513/com.landptf.blog E/MainFragment: --- 4 ---
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。