如何使广告在后台加载,因为现在滞后

时间:2018-02-26 10:46:11

标签: android

我正在制作一个GymExercises应用程序。我有两个问题:

  1. 我在每个练习中都包含广告,但现在用户界面太多了。如何将它们放在后台以便它们在后台运行?

  2. 当我使用智能横幅时,有时它不会显示广告。使用小横幅,它可以很好地工作。

  3. BenchFragment.java:

    package com.Hristijan.Aleksandar.GymAssistant.Exercises; 
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Handler;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.MediaController;
    import android.widget.VideoView;
    import com.google.android.gms.ads.MobileAds;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView; 
    
    public class BenchFragment extends Fragment {
    
        private VideoView player;
        private String videopath;
        private MediaController mediacon;
        public  View rootView;
        private AdView mAdView;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    
            rootView = inflater.inflate(R.layout.fragment_bench, container, false);
            mAdView = rootView.findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            MobileAds.initialize(getActivity().getApplicationContext(), "ca-app-pub-7751214307362146~7028081975");
            mAdView.loadAd(adRequest);
            mediacon = new MediaController(getActivity());
            player = (VideoView) rootView.findViewById(R.id.videoplayer);
            videopath = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.bench;
            player.setVideoURI(Uri.parse(videopath));
            mediacon.setAnchorView(player);
            player.start();
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.setLooping(true);
                    mp.setVolume(0, 0);
                }
            });
            return rootView;
        }
    }
    

    fragment_bench.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/bench_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:fillViewport="true"
        tools:context="com.Hristijan.Aleksandar.GymAssistant.Exercises.BenchFragment">
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
    
                <VideoView
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_160sdp"
                    android:foregroundGravity="center"
                    android:id="@+id/videoplayer" />
    
                <TableLayout
                    android:layout_below="@+id/videoplayer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:shrinkColumns="*">
    
                <TableRow>
                    <TextView
                        android:text="@string/group"
                        android:background="@color/colorPrimary"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:textColor="#ffffff"
                        android:textSize="@dimen/_10sdp"
                        android:padding="5dip" />
                </TableRow>
    
                <TableRow>
                    <TextView
                        android:text="@string/bencbody"
                        android:textSize="@dimen/_10sdp"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:textColor="#ffffff"
                        android:padding="5dip" />
                </TableRow>
    
                <TableRow>
                    <TextView
                        android:text="@string/description"
                        android:textSize="@dimen/_10sdp"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@color/colorPrimary"
                        android:textColor="#ffffff"
                        android:padding="5dip" />
                </TableRow>
    
                <TableRow>
                    <TextView
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="left"
                        android:layout_marginTop="5dp"
                        android:text="@string/stepone"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/_10sdp"
                        android:padding="3dip" />
                </TableRow>
    
                <TableRow>
                    <TextView
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="5dp"
                        android:gravity="left"
                        android:text="@string/steptwo"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/_10sdp"
                        android:padding="3dip" />
                </TableRow>
            </TableLayout>
    
            <com.google.android.gms.ads.AdView
                xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                ads:adSize="BANNER"
                ads:adUnitId="ca-app-pub-7751214307362146/9071791385">
            </com.google.android.gms.ads.AdView>    
        </RelativeLayout>
    </ScrollView>
    

0 个答案:

没有答案