Admob Android делает сетевые вызовы, даже если приложение находится в фоновом режиме, даже если вызывается Adview.pause (). Это нормально? - PullRequest
0 голосов
/ 20 марта 2020

У меня есть приложение android, которое имеет стандартный макет родительского основного действия, содержащего три вкладки (каждая имеет фрагмент) и вид Admob Ad в нижней части.

Насколько я знаю, я реализовал все необходимые события жизненного цикла Admob для AdView, но я заметил, что Admob все еще выполняет сетевые вызовы, даже если приложение находится в фоновом режиме, что влияет на срок службы батареи.

Поскольку я хочу использовать новый Admob Адаптивный размер баннера. Я создаю объявления в своем коде java, а затем динамически добавляю их в действие. Вот SDK-версии Admob и Facebook (я использую Facebook при посредничестве Admob)

implementation 'com.google.android.gms:play-services-ads:18.3.0'
implementation 'com.android.support:support-annotations:28.0.0' // Required Dependency by Audience Network SDK
implementation 'com.facebook.android:audience-network-sdk:5.7.1'
implementation 'com.google.ads.mediation:facebook:5.7.1.0'

Вот студийный профилировщик android, который показывает сетевые вызовы, даже когда приложение находится в фоновом режиме. (Кроме того, у меня за раз 8–15 открытых сетевых подключений, это нормально?) enter image description here

Ниже приведены некоторые примеры кода:

С Java Упражнение

 public class MainActivity extends AppCompatActivity implements IabBroadcastListener {


LinearLayout adLinearLayout = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AdView mAdView = new AdView(this);
    if (!"Y".equals(isUserProFlagFromResourceForAds)) {
        AdSize adSize = getAdSize();
        mAdView.setAdSize(adSize);
        mAdView.setAdUnitId("AD_ID_GOES_HERE");

        adLinearLayout.addView(mAdView);
        adLinearLayout.setMinimumHeight(adSize.getHeightInPixels(getApplicationContext()));
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }

}


//I made sure in the below that retreiving the AdView object from the adLinearLayout always works
//and that it is never null, and that the destroy (and other) methods are invoked.
//Also made sure at any time there is 1 and only 1 AdView object in its container lineary layout.
@Override
public void onDestroy() {

    AdView _adView = Utils.getAdViewFromLinearLayout(adLinearLayout);
    if (_adView != null) {
        _adView.destroy();
    }
    super.onDestroy();

}


@Override
public void onResume() {

    super.onResume();

    AdView _adView = Utils.getAdViewFromLinearLayout(adLinearLayout);

    if (_adView != null) {
        _adView.resume();
    }


}

@Override
public void onPause() {
    AdView _adView = Utils.getAdViewFromLinearLayout(adLinearLayout);

    if (_adView != null) {
        _adView.pause();
    }
    super.onPause();
}

}

Из макета

                <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"
                xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:layoutDirection = "locale"
                android:orientation="vertical"
                android:background="@color/colorWhite"
                android:id="@+id/mainRelLayout"
                tools:context="com.tototototototototototototototot.MainActivity">
            <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layoutDirection = "locale"
                android:layout_width="match_parent"
                xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:layout_above="@+id/main_activity_ad_linear_layout"
                android:layout_height="match_parent">
                <com.google.android.material.appbar.AppBarLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
                    <androidx.appcompat.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="?attr/colorPrimary"
                        android:textStyle="bold"
                        app:layout_scrollFlags="scroll|enterAlways"
                        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
                    <com.google.android.material.tabs.TabLayout
                        android:id="@+id/tabs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:tabMode="fixed"
                        app:tabTextColor="@color/unselectedTabText"
                        app:tabSelectedTextColor="@color/selectedTabText"
                        app:tabIndicatorColor="@color/tabIndicator"
                        android:textStyle="bold"
                        app:tabGravity="fill"/>
                </com.google.android.material.appbar.AppBarLayout>
                <androidx.viewpager.widget.ViewPager
                        android:id="@+id/viewpager"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        app:layout_behavior="com.totototoototo.FixScrollingFooterBehavior">
                    </androidx.viewpager.widget.ViewPager>
            </androidx.coordinatorlayout.widget.CoordinatorLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_centerHorizontal="true"
                    android:focusableInTouchMode="true"
                    android:layout_alignParentBottom="true"
                    android:id="@+id/main_activity_ad_linear_layout">
                </LinearLayout>
            </RelativeLayout>

При необходимости вот макет приложения.

enter image description here

...