如何在互联网不可用时隐藏广告

时间:2012-08-12 04:35:52

标签: android

我正在制作一个应用程序,我正在给它添加。但即使互联网连接不可用,它也需要在视图/布局中占用空间。我只想隐藏它并且不允许它在布局中占用空间/当互联网不可用时查看。这是xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible" >

            <ImageView
                android:id="@+id/explain_QuestionImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="visible" />

            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5.0dip"
                android:layout_marginLeft="1.0dip"
                android:layout_marginRight="1.0dip"
                android:layout_marginTop="1.0dip"
                android:fadeScrollbars="true"
                android:fadingEdge="none"
                android:fillViewport="true" >

                <TextView
                    android:id="@+id/explain_question"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_below="@id/explain_QuestionImageView"
                    android:fadingEdge="vertical"
                    android:scrollbars="vertical"
                    android:text=""
                    android:textColor="@color/question_text"
                    android:textColorHighlight="@color/question_text1" />
            </ScrollView>

            <TextView
                android:id="@+id/explain_separator"
                android:layout_width="match_parent"
                android:layout_height="5.0dip"
                android:layout_alignParentRight="true"
                android:layout_below="@id/explain_question"
                android:background="@drawable/blue_gradient"
                android:fadingEdge="vertical"
                android:padding="10dp"
                android:scrollbars="vertical"
                android:text="" />

            <ScrollView
                android:id="@+id/scrollView2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="100.0dip"
                android:layout_marginLeft="1.0dip"
                android:layout_marginRight="1.0dip"
                android:layout_marginTop="1.0dip"
                android:fadeScrollbars="true"
                android:fadingEdge="none"
                android:fillViewport="true" >

                <TextView
                    android:id="@+id/explain_explaination"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_below="@id/explain_separator"
                    android:fadingEdge="vertical"
                    android:padding="10dp"
                    android:scrollbars="vertical"
                    android:text=""
                    android:textColor="@color/dark_green" />
            </ScrollView>
        </LinearLayout>
        <!-- </ScrollView> -->

    </FrameLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
       >

        <com.google.ads.AdView
            android:id="@+id/ad"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:keepScreenOn="true"
            ads:adSize="BANNER"
            ads:adUnitId="a1501e5633125fb"
            ads:loadAdOnCreate="true"
            ads:testDevices="TEST_EMULATOR, 123456789ABCDEF" >
        </com.google.ads.AdView>

        <LinearLayout
            android:id="@+id/LL_Buttons1"
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:layout_above="@id/ad"
            android:layout_gravity="bottom"
            android:background="#ff777777"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/PrevExplainationButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_marginLeft="2.0dip"
                android:layout_weight="1.0"
                android:background="@drawable/ibtn"
                android:onClick="onPrevExplainationButtonClick"
                android:text="Previous" />

            <Button
                android:id="@+id/NextExplainationButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_marginLeft="5.0dip"
                android:layout_marginRight="2.0dp"
                android:layout_weight="1.0"
                android:background="@drawable/ibtn"
                android:onClick="onNextExplainationButtonClick"
                android:text="Next" />
        </LinearLayout>
    </RelativeLayout>

请告诉我该怎么做。

4 个答案:

答案 0 :(得分:2)

在这里,举一个更具体的例子,您可以测试连接,然后将可见性设置为gone

private boolean isNetworkConnected() {
    yourAddView = (AdView) findViewById(R.id.ad);
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni == null) {
      // There are no active networks.
      //Set the visibility to "gone".
      //You can set visibility to gone here or when the function returns,
      //that is why there is a return false and true. 
      myView.setVisibility(8);
      return false;
    } else
    return true;
}

您还需要将这些权限添加到清单文件中:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

希望有所帮助。

答案 1 :(得分:0)

要检查数据连接是否可用,请使用以下功能

还在Manifest中添加权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在活动中:

public boolean netCheckin() {

try {

    ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    nInfo.getActiveNetworkInfo().isConnectedOrConnecting();

    // Log.d(tag, "Net avail:"
       // + nInfo.getActiveNetworkInfo().isConnectedOrConnecting());

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        //Log.d(tag, "Network available:true");

        return true;
    } else {
    // Log.d(tag, "Network available:false");

        return false;
    }

} catch (Exception e) {

    return false;
}
}

答案 2 :(得分:0)

试试这个

    ad.setAdListener(new AdListener() {

    @Override
    public void onAdLoaded() {
        ad.setVisibility(View.VISIBLE);
    }

    @Override
    public void onAdFailedToLoad(int error) {
        ad.setVisibility(View.GONE);
    }    

});

答案 3 :(得分:0)

添加Abed Putra的answer的Kotlin版本

MobileAds.initialize(this)
var madView = findViewById<AdView>(R.id.adView)
madView.loadAd(AdRequest.Builder().build())
madView.adListener = object: AdListener(){
    override fun onAdFailedToLoad(p0: Int) {
        madView.visibility = GONE
    }
}