Issue
I implemented AppOpen to my project, it works fine. Here's the code:
public class AppOpenManager implements LifecycleObserver, Application.ActivityLifecycleCallbacks {
private static final String LOG_TAG = "AppOpenManager";
private static String AD_UNIT_ID;
private AppOpenAd appOpenAd = null;
private static boolean isShowingAd = false;
public static boolean shouldOpenAd = true;
private AppOpenAd.AppOpenAdLoadCallback loadCallback;
private Activity currentActivity;
private long loadTime = 0;
private final UILApplication myApplication;
public AppOpenManager(UILApplication myApplication, boolean isPurchased) {
AD_UNIT_ID = AdsConstants.getAppOpenId(myApplication.getApplicationContext());
this.myApplication = myApplication;
this.myApplication.registerActivityLifecycleCallbacks(this);
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
shouldOpenAd = !isPurchased;
}
/**
* LifecycleObserver methods
*/
@OnLifecycleEvent(ON_START)
public void onStart() {
if (shouldOpenAd)
showAdIfAvailable();
Log.d(LOG_TAG, "onStart");
}
/**
* Request an ad
*/
public void fetchAd() {
// Have unused ad, no need to fetch another.
if (isAdAvailable()) {
return;
}
loadCallback = new AppOpenAd.AppOpenAdLoadCallback() {
/**
* Called when an app open ad has loaded.
* @param ad the loaded app open ad.
*/
@Override
public void onAdLoaded(@NonNull AppOpenAd ad) {
AppOpenManager.this.appOpenAd = ad;
AppOpenManager.this.loadTime = (new Date()).getTime();
Log.d("OnAdLoaded", "Banner adapter class name: " + ad.getResponseInfo().getMediationAdapterClassName());
}
/**
* Called when an app open ad has failed to load.
* @param loadAdError the error.
*/
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
}
};
AdRequest request = getAdRequest();
AppOpenAd.load(
myApplication, AD_UNIT_ID, request,
AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback);
}
/**
* Shows the ad if one isn't already showing.
*/
public void showAdIfAvailable() {
// Only show ad if there is not already an app open ad currently showing
// and an ad is available.
if (!isShowingAd && isAdAvailable()) {
Log.d(LOG_TAG, "Will show ad.");
FullScreenContentCallback fullScreenContentCallback =
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Set the reference to null so isAdAvailable() returns false.
AppOpenManager.this.appOpenAd = null;
isShowingAd = false;
fetchAd();
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
}
@Override
public void onAdShowedFullScreenContent() {
isShowingAd = true;
}
};
appOpenAd.setFullScreenContentCallback(fullScreenContentCallback);
appOpenAd.show(currentActivity);
} else {
Log.d(LOG_TAG, "Can not show ad.");
fetchAd();
}
}
/**
* Creates and returns ad request.
*/
private AdRequest getAdRequest() {
return new AdRequest.Builder().build();
}
/**
* Utility method that checks if ad exists and can be shown.
*/
public boolean isAdAvailable() {
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
}
/**
* Utility method to check if ad was loaded more than n hours ago.
*/
private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
long dateDifference = (new Date()).getTime() - this.loadTime;
long numMilliSecondsPerHour = 3600000;
return (dateDifference < (numMilliSecondsPerHour * numHours));
}
//some listeners here
}
The only problem is that I have Activity Result:
someActivityResultLauncher = fragment.registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
if (data != null) {
ArrayList<Uri> uriList = new ArrayList<>();
if (data.getClipData() != null) {
int count = data.getClipData().getItemCount();
for (int i = 0; i < count; i++) {
Uri uri = data.getClipData().getItemAt(i).getUri();
uriList.add(uri);
}
} else if (data.getData() != null) {
Uri uri = data.getData();
uriList.add(uri);
}
intentServiceSendingFileUtils.createIntentForSendingFile(fragment.requireActivity(),
uriList, info);
}
}
});
Every time I want to select pictures/documents/etc. AppOpen Ad appears. How to prevent my app from doing this?
I want to show App Open Ad only when my app opens or user goes back. I can set some boolean value to true/false to show or not the ad but maybe there is any better solution for this.
Solution
okay, fixed, maybe anyone needs this:
@OnLifecycleEvent(ON_START)
public void onStart() {
Log.e(LOG_TAG, "onStart isSelectingFile --> " + isSelectingFile);
if (shouldOpenAd && !isSelectingFile) {
showAdIfAvailable();
} else if (isSelectingFile)
isSelectingFile = false;
}
and
private void getContent(String type) {
AppOpenManager.isSelectingFile = true;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setType(type);
someActivityResultLauncher.launch(intent);
}
Just set the boolean before you open Gallery or File Manager and change it when you already got result what you wanted.
Answered By - Marina
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.