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
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
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(); } 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() { @Override public void onADDismissed() { activity.dismissSplash(); } @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"); } @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 ---
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网行业资讯频道,感谢您对编程网的支持。