实现SwipeRefreshLayout和奖励视频的问题

时间:2019-03-21 12:18:08

标签: android swiperefreshlayout admob-rewardedvideoad

在创建应用程序的过程中,我遇到了一些代码问题,最重要的是,用户打开应用程序时按下“获取”按钮,观看广告视频并收到促销代码,但是如果该人关闭广告或收到促销代码而无法打开电影,则面临一个问题,第二个问题与页面更新的实现有关;我使用了SwipeRefresh布局动画,但是没有更新。

应用代码:

public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {

    private static final String TAG = "MainActivity";

    AdView mAdView;

    Button mButtonGive, mButtonOrig, mButtonAmedia, mButtonIvi;
    TextView mTextPromo;

    RewardedVideoAd mAd;

    ClipboardManager clipboardManager;
    ClipData clipData;

    FirebaseFirestore mRef = FirebaseFirestore.getInstance();
    DocumentReference mPapaRef = mRef.collection("Promocode").document("Papa_Johns");

    SwipeRefreshLayout mSwipeRefresh;


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

        mButtonGive = findViewById(R.id.mButtonGivePapa);
        mTextPromo = findViewById(R.id.mTextPromoPapa);
        mButtonOrig = findViewById(R.id.mButtonBuyOrig);
        mButtonAmedia = findViewById(R.id.mButtonBuyAmedia);
        mButtonIvi = findViewById(R.id.mButtonBuyIvi);

        mSwipeRefresh = findViewById(R.id.swipeRefresh);
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.i(TAG, "onRefresh called from SwipeRefreshLayout");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeRefresh.setRefreshing(false);
                    }
                }, 3000);
            }
        });
        mSwipeRefresh.setColorSchemeResources(R.color.colorLight, R.color.colorMiddle, R.color.colorPrimary);

        MobileAds.initialize(this, "ca-app-pub-7120970387686966~9640430308");
        mAd = MobileAds.getRewardedVideoAdInstance(this);
        mAd.setRewardedVideoAdListener(this);

        mAd.loadAd("ca-app-pub-7120970387686966/4195342567", new AdRequest.Builder().build());


        MobileAds.initialize(this, "ca-app-pub-7120970387686966/2942750897");

        mAdView = findViewById(R.id.mBanner);
        AdRequest adRequest1 = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest1);


        mButtonGive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextPromo.setText("");
                mPapaRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            DocumentSnapshot document = task.getResult();
                            assert document != null;
                            if (document.exists()) {
                                List<String> promocodes = (List<String>) document.get("Promo");
                                if (promocodes.size() > 0) {
                                    if (mAd.isLoaded()){
                                        mAd.show();
                                    }
                                } else {
                                    mTextPromo.setText("Купонов нет ;(");
                                }
                            } else {
                                Log.d(TAG, "No such document");
                            }

                        }
                        else {
                            Log.d(TAG, "get failed with ", task.getException());
                        }
                    }

                });

            }
        });

        clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        mTextPromo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = mTextPromo.getText().toString();
                clipData = ClipData.newPlainText("text", text);
                clipboardManager.setPrimaryClip(clipData);

                Toast.makeText(getApplicationContext(),"Скопировано в буфер обмена", Toast.LENGTH_SHORT).show();
            }
        });

        mButtonOrig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent broIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://clc.to/origin_gift"));
                    startActivity(broIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        mButtonAmedia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent broIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://clc.to/amediateka_gift"));
                    startActivity(broIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        mButtonIvi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent broIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://clc.to/ivi_account"));
                    startActivity(broIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    @Override
    public void onRewardedVideoAdLoaded() {
    }

    @Override
    public void onRewardedVideoAdOpened() {

    }

    @Override
    public void onRewardedVideoStarted() {

    }

    @Override
    public void onRewardedVideoAdClosed() {

    }

    @Override
    public void onRewarded(RewardItem rewardItem) {

    }

    @Override
    public void onRewardedVideoAdLeftApplication() {

    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int i) {

    }

    @Override
    public void onRewardedVideoCompleted() {
        mPapaRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    assert document != null;
                    if (document.exists()) {
                        List<String> promocodes = (List<String>) document.get("Promo");
                        if (promocodes.size() > 0) {
                            int a = new Random().nextInt(promocodes.size());
                            mPapaRef.update("Promo", FieldValue.arrayRemove(promocodes.get(a)));
                            mTextPromo.setText(promocodes.get(a));
                        }
                    }
                }
            }
        });
    }
}

Logcat:

  

W /广告:#004 Web视图被破坏。忽略动作。

     

我/聊天:uid = 10091(ru.redstart)相同的6行

     

W /广告:#004 Web视图被破坏。忽略动作。

     

广告:没有足够的空间来展示广告。需要412x90 dp,但只有411x718 dp。

     

广告:没有足够的空间来展示广告。需要412x90 dp,但只有53x718 dp。

     

广告:没有足够的空间来展示广告。需要412x90 dp,但只有411x694 dp。

     

广告:没有足够的空间来展示广告。需要412x90 dp,但只有53x694 dp。

     

D / EGL_emulation:eglMakeCurrent:0xdcf64ea0:ver 2 0(tinfo 0xc2221880)

     

I / Ads:此请求是从测试设备发送的。

     

I / Ads:SDK版本:afma-sdk-a-v15000000.15000000.0

     

I /广告:广告无法加载:3

我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ru.redstart">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-7120970387686966~9640430308"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

0 个答案:

没有答案