在哪里可以调用admob广告?

时间:2018-06-26 08:11:53

标签: java android android-studio admob google-admob

我正在为一个 android项目工作,我有两个文件,我必须在其中调用 Admob Ad ,我正在共享两个我认为可以调用的资源 Admob广告。 尝试解决它...


BaseAuthActivity.java

package com.bunseinc.bunseapp.ui.activities.authorization;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.util.Log;
import android.widget.EditText;

import com.afollestad.materialdialogs.MaterialDialog;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginResult;
import com.firebase.ui.auth.ErrorCodes;
import com.firebase.ui.auth.IdpResponse;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.quickblox.auth.model.QBProvider;
import com.bunseinc.bunseapp.R;
import com.bunseinc.bunseapp.ui.activities.base.BaseActivity;
import com.bunseinc.bunseapp.ui.activities.main.MainActivity;
import com.bunseinc.bunseapp.ui.fragments.dialogs.UserAgreementDialogFragment;
import com.bunseinc.bunseapp.utils.AuthUtils;
import com.bunseinc.bunseapp.utils.StringObfuscator;
import com.bunseinc.bunseapp.utils.helpers.FlurryAnalyticsHelper;
import com.bunseinc.bunseapp.utils.helpers.GoogleAnalyticsHelper;
import com.bunseinc.bunseapp.utils.helpers.FacebookHelper;
import com.bunseinc.bunseapp.utils.helpers.FirebaseAuthHelper;
import com.quickblox.q_municate_core.models.AppSession;
import com.quickblox.q_municate_core.models.LoginType;
import com.bunseinc.bunseapp.utils.helpers.ServiceManager;
import com.quickblox.users.model.QBUser;

import butterknife.Bind;
import butterknife.OnTextChanged;
import rx.Observer;

public abstract class BaseAuthActivity extends BaseActivity {

  private static String TAG = BaseAuthActivity.class.getSimpleName();

  protected static final String STARTED_LOGIN_TYPE = "started_login_type";

  @Nullable
  @Bind(R.id.email_textinputlayout)
  protected TextInputLayout emailTextInputLayout;

  @Nullable
  @Bind(R.id.email_edittext)
  protected EditText emailEditText;

  @Nullable
  @Bind(R.id.password_textinputlayout)
  protected TextInputLayout passwordTextInputLayout;

  @Nullable
  @Bind(R.id.password_edittext)
  protected EditText passwordEditText;

  protected FacebookHelper facebookHelper;
  private FirebaseAuthHelper firebaseAuthHelper;
  private FirebaseAuthCallback firebaseAuthCallback;

  protected LoginType loginType = LoginType.EMAIL;
  protected Resources resources;
  private AdView adView;
  private ServiceManager serviceManager;
  private AdRequest adRequest;
  public static void start(Context context) {
    Intent intent = new Intent(context, BaseAuthActivity.class);
    context.startActivity(intent);
  }

  protected void startad() {
    MobileAds.initialize(this,
      "ca-app-pub-3940256099942544~3347511713");
    adView = findViewById(R.id.adView);
    adRequest = new AdRequest.Builder().build();
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initFields(savedInstanceState);
    startad();
  }

  @Override
  public void onStart() {
    super.onStart();
    facebookHelper.onActivityStart();
    if (adView != null) {
      adView.loadAd(adRequest);
    }
  }


  @Override
  public void onStop() {
    super.onStop();
    facebookHelper.onActivityStop();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable(STARTED_LOGIN_TYPE, loginType);
  }


  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "onActivityResult, result code = " + requestCode);
    if (requestCode == FirebaseAuthHelper.RC_SIGN_IN) {
      onReceiveFirebaseAuthResult(resultCode, data);
    }
    facebookHelper.onActivityResult(requestCode, resultCode, data);
  }

  private void onReceiveFirebaseAuthResult(int resultCode, Intent data) {
    IdpResponse response = IdpResponse.fromResultIntent(data);

    // Successfully signed in
    if (resultCode == RESULT_OK) {
      firebaseAuthHelper.refreshInternalFirebaseToken(firebaseAuthCallback);
    } else {
      //Sign in failed
      if (response == null) {
        // User pressed back button
        Log.i(TAG, "BACK button pressed");
        return;
      }

      if (response.getErrorCode() == ErrorCodes.NO_NETWORK || response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
        showSnackbar(R.string.dlg_internet_connection_error, Snackbar.LENGTH_INDEFINITE);
      }
    }
  }

  @Nullable
  @OnTextChanged(R.id.email_edittext)
  void onTextChangedEmail(CharSequence text) {
    emailTextInputLayout.setError(null);
  }

  @Nullable
  @OnTextChanged(R.id.password_edittext)
  void onTextChangedPassword(CharSequence text) {
    passwordTextInputLayout.setError(null);
  }

  private void initFields(Bundle savedInstanceState) {
    resources = getResources();
    if (savedInstanceState != null && savedInstanceState.containsKey(STARTED_LOGIN_TYPE)) {
      loginType = (LoginType) savedInstanceState.getSerializable(STARTED_LOGIN_TYPE);
    }
    facebookHelper = new FacebookHelper(this);
    firebaseAuthHelper = new FirebaseAuthHelper(BaseAuthActivity.this);
    firebaseAuthCallback = new FirebaseAuthCallback();
    failAction = new FailAction();
    serviceManager = ServiceManager.getInstance();
  }

  protected void startSocialLogin() {
    if (!appSharedHelper.isShownUserAgreement()) {
      UserAgreementDialogFragment
        .show(getSupportFragmentManager(), new MaterialDialog.ButtonCallback() {
          @Override
          public void onPositive(MaterialDialog dialog) {
            super.onPositive(dialog);
            appSharedHelper.saveShownUserAgreement(true);
            loginWithSocial();
          }
        });
    } else {
      loginWithSocial();
    }
  }

  private void loginWithSocial() {
    appSharedHelper.saveFirstAuth(true);
    appSharedHelper.saveSavedRememberMe(true);
    if (loginType.equals(LoginType.FACEBOOK)) {
      facebookHelper.login(new FacebookLoginCallback());
    } else if (loginType.equals(LoginType.FIREBASE_PHONE)) {
      appSharedHelper.saveFerebaseProjectId(StringObfuscator.getFirebaseAuthProjectId());
      firebaseAuthHelper.loginByPhone(BaseAuthActivity.this);
    }
  }

  protected void startMainActivity(QBUser user) {
    AppSession.getSession().updateUser(user);
    startMainActivity();
  }

  protected void startMainActivity() {
    MainActivity.start(BaseAuthActivity.this);

    finish();
  }

  protected void login(String userEmail, final String userPassword) {
    appSharedHelper.saveFirstAuth(true);
    appSharedHelper.saveSavedRememberMe(true);
    appSharedHelper.saveUsersImportInitialized(true);
    QBUser user = new QBUser(null, userPassword, userEmail);

    serviceManager.login(user).subscribe(new Observer < QBUser > () {
      @Override
      public void onCompleted() {

      }

      @Override
      public void onError(Throwable e) {
        Log.d(TAG, "onError" + e.getMessage());
        hideProgress();
        AuthUtils.parseExceptionMessage(BaseAuthActivity.this, e.getMessage());
      }

      @Override
      public void onNext(QBUser qbUser) {
        performLoginSuccessAction(qbUser);
      }
    });
  }

  protected void startLandingScreen() {
    LandingActivity.start(this);
    finish();
  }

  private void performLoginSuccessAction(QBUser user) {
    startMainActivity(user);
    // send analytics data
    GoogleAnalyticsHelper.pushAnalyticsData(this, user, "User Sign In");
    FlurryAnalyticsHelper.pushAnalyticsData(this);
  }


  private Observer < QBUser > socialLoginObserver = new Observer < QBUser > () {
    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {
      Log.d(TAG, "onError " + e.getMessage());
      hideProgress();
      AuthUtils.parseExceptionMessage(BaseAuthActivity.this, e.getMessage());
    }

    @Override
    public void onNext(QBUser qbUser) {
      performLoginSuccessAction(qbUser);
    }
  };

  private class FacebookLoginCallback implements FacebookCallback < LoginResult > {

    @Override
    public void onSuccess(LoginResult loginResult) {
      Log.d(TAG, "+++ FacebookCallback call onSuccess from BaseAuthActivity +++");
      showProgress();
      serviceManager.login(QBProvider.FACEBOOK, loginResult.getAccessToken().getToken(), null)
        .subscribe(socialLoginObserver);
    }

    @Override
    public void onCancel() {
      Log.d(TAG, "+++ FacebookCallback call onCancel from BaseAuthActivity +++");
      hideProgress();
    }

    @Override
    public void onError(FacebookException error) {
      Log.d(TAG, "+++ FacebookCallback call onCancel BaseAuthActivity +++");
      hideProgress();
    }
  }

  private class FirebaseAuthCallback implements FirebaseAuthHelper.RequestFirebaseIdTokenCallback {

    @Override
    public void onSuccess(String authToken) {
      Log.d(TAG, "FirebaseAuthCallback onSuccess()");
      showProgress();
      serviceManager.login(QBProvider.FIREBASE_PHONE, authToken, appSharedHelper.getFirebaseProjectId())
        .subscribe(socialLoginObserver);
    }

    @Override
    public void onError(Exception e) {
      Log.d(TAG, "FirebaseAuthCallback onError()");
      hideProgress();
    }
  }
}

LandingActivity.java

package com.bunseinc.bunseapp.ui.activities.authorization;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bunseinc.bunseapp.R;
import com.bunseinc.bunseapp.utils.StringObfuscator;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.quickblox.q_municate_core.models.LoginType;

import butterknife.Bind;
import butterknife.OnClick;

public class LandingActivity extends BaseAuthActivity {

  @Bind(R.id.app_version_textview)
  TextView appVersionTextView;

  @Bind(R.id.phone_number_connect_button)
  Button phoneNumberConnectButton;
  public static void start(Context context) {
    Intent intent = new Intent(context, LandingActivity.class);
    context.startActivity(intent);
  }

  public static void start(Context context, Intent intent) {
    context.startActivity(intent);
  }
  @Override
  protected int getContentResId() {
    return R.layout.activity_landing;
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initVersionName();
  }
  @OnClick(R.id.login_button)
  void login(View view) {
    LoginActivity.start(LandingActivity.this);
    finish();
  }
  @OnClick(R.id.phone_number_connect_button)
  void phoneNumberConnect(View view) {
    if (checkNetworkAvailableWithError()) {
      loginType = LoginType.FIREBASE_PHONE;
      startSocialLogin();
    }
  }
  @Override
  public void checkShowingConnectionError() {
    // nothing. Toolbar is missing.
  }

  private void startSignUpActivity() {
    SignUpActivity.start(LandingActivity.this);
    finish();
  }
  private void initVersionName() {
    appVersionTextView.setText(StringObfuscator.getAppVersionName());
  }
}

注意:如果我将public void onCreate更改为Protected void onCreate,则会由于调用public void onCreate而导致整个项目出错。

预先感谢

0 个答案:

没有答案