在onActivityResult中调用Intent后App Open Ad显示,如何停止
App Open Ad shows after Intent was called in onActivityResult, how to stop it
我在我的项目中实现了 AppOpen,它运行良好。
这是代码:
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
}
唯一的问题是我有 Activity 结果:
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);
}
}
});
每次都想selectpictures/documents/etc。 AppOpen 广告出现。
如何防止我的应用执行此操作?
我只想在我的应用打开或用户返回时展示应用打开广告。
我可以将一些布尔值设置为 true/false 以显示或不显示广告,但也许对此有更好的解决方案。
好的,修复了,也许有人需要这个:
@OnLifecycleEvent(ON_START)
public void onStart() {
Log.e(LOG_TAG, "onStart isSelectingFile --> " + isSelectingFile);
if (shouldOpenAd && !isSelectingFile) {
showAdIfAvailable();
} else if (isSelectingFile)
isSelectingFile = false;
}
和
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);
}
只需在打开图库或文件管理器之前设置布尔值,并在获得所需结果后更改它。
我在我的项目中实现了 AppOpen,它运行良好。 这是代码:
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
}
唯一的问题是我有 Activity 结果:
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);
}
}
});
每次都想selectpictures/documents/etc。 AppOpen 广告出现。 如何防止我的应用执行此操作?
我只想在我的应用打开或用户返回时展示应用打开广告。 我可以将一些布尔值设置为 true/false 以显示或不显示广告,但也许对此有更好的解决方案。
好的,修复了,也许有人需要这个:
@OnLifecycleEvent(ON_START)
public void onStart() {
Log.e(LOG_TAG, "onStart isSelectingFile --> " + isSelectingFile);
if (shouldOpenAd && !isSelectingFile) {
showAdIfAvailable();
} else if (isSelectingFile)
isSelectingFile = false;
}
和
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);
}
只需在打开图库或文件管理器之前设置布尔值,并在获得所需结果后更改它。