I'm trying to do admost interstitial integration into my application. There are tab actions in my application, so there is bottom navigation at the bottom of the screen, and when they are clicked, interstitial should appear, at least I think I coded it that way, but it never loads because interstitial.isLoaded is null. interstitial does not come null activity , does not come null , but isLoaded does not show at all because it is always false . I'm sharing my codes can you help me with this I'm stuck here
I only share the necessary stuff because I have set up an abstract factory structure and it would be too complicated to share the whole structure
public void ifNeededShowinterstitial(){
if(UserConfig.enabledAds && UserConfig.enabledInterstitialAds){
DYTIntersitialProvider interstitial = DYTBannerFactory.shared().interstitial(DYTBannerFactory.shared().getBannerIdContainer().interstitial());
if(interstitial instanceof DYTBannerActivityClient){
((DYTBannerActivityClient) interstitial).setActivity(BaseActivity.mainActivity);
}
interstitial.load();
}
}
public void gotoExerciseActivity() {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
Intent i = new Intent(MainActivity.this, ExerciseActivity.class);
i.addFlags(Intent.FLAG_FROM_BACKGROUND);
startActivity(i);
//overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
ifNeededShowinterstitial();
}
For example, in this code, when the exercise button on the navigation bar is clicked, it calls the ifNeededShowinterstitial function at the bottom, which calls load.
AdMostInterstitial interstitial;
Activity activity;
#Override
public void setActivity(Activity activity) {
this.activity = activity;
}
#Override
public void load() {
if (interstitial == null) {
AdMostAdListener listener = new AdMostAdListener() {
#Override
public void onDismiss(String message) {
Log.d(Statics.TAG, "MainActivity interstitial onDismiss network: " + message);
}
#Override
public void onComplete(String s) {
Log.d(Statics.TAG, "MainActivity interstitial onComplete network: " + s);
}
#Override
public void onFail(int errorCode) {
logError(errorCode, "interstitial");
}
#Override
public void onReady(String network, int ecpm) {
Log.d(Statics.TAG, "MainActivity interstitial onReady network : " + network);
}
#Override
public void onShown(String network) {
Log.d(Statics.TAG, "MainActivity interstitial onShown network: " + network);
}
#Override
public void onClicked(String s) {
Log.d(Statics.TAG, "MainActivity interstitial onClicked network: " + s);
}
#Override
public void onStatusChanged(int i) {
}
};
interstitial = new AdMostInterstitial(this.activity, Statics.FULLSCREEN_ZONE, listener);
}
interstitial.refreshAd(false);
if(interstitial != null && interstitial.isLoaded())
interstitial.show();
}
Every time these navigation buttons are clicked, load is called and these codes work. I did the same codes in the documentation codes. What could be the problem? Is there something missing or am I doing something wrong I couldn't figure it out please help?
you can see it here in the documentation: https://admost.github.io/amrandroid/
Related
I am trying to implement interstitial ad in a fragment, when button is clicked the fragment doesn't open other activity , but loads data in same fragment so i can't use the they way people say interstisial ad should be used when navigating between activities. instead i am using the ad for only once when button is clicked .it works but takes sometime .how to deal with it?
it only works if user pressed the button after few seconds of launching the app.
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_home, container, false);
Button btn=root.findViewById(R.id.btn);
initAds();
btn.setOnClickListener(this);
return root;
}
#Override
public void onClick(View view){
initAdsCallBack();
}
private void initAds(){
MobileAds.initialize(requireActivity(), new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(#NonNull InitializationStatus initializationStatus) {
createNonPersonalisedAd();
}
});
}
}
private void createNonPersonalisedAd() {
Bundle networkExtrasBundle = new Bundle();
networkExtrasBundle.putInt("rdp", 1);
AdManagerAdRequest adRequest = (AdManagerAdRequest) new AdManagerAdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, networkExtrasBundle)
.build();
createInterstitialAd(adRequest);
}
private void createInterstitialAd(AdRequest adRequest){
AdManagerInterstitialAd.load(requireActivity(),getResources().getString(R.string.str_iterstitial), (AdManagerAdRequest) adRequest,
new AdManagerInterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull AdManagerInterstitialAd interstitialAd) {
// The mAdManagerInterstitialAd reference will be null until
// an ad is loaded.
mAdManagerInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
///// best place to callback is here coz its successfully loaded here
mAdManagerInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
#Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d("TAG", "The ad was dismissed.");
createInterstitialAd(adRequest);
}
#Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d("TAG", "The ad failed to show.");
}
#Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mAdManagerInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
}
});
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mAdManagerInterstitialAd = null;
}
});
}
private void initAdsCallBack(){
if (mAdManagerInterstitialAd != null) {
mAdManagerInterstitialAd.show(requireActivity());
debugToast("ad shown");
} else {
Log.e("Tad didn't show");
}
}
Consider pre-loading your interstitial ads to reduce latency when displaying them to your users. For more information about pre-loading your interstitial ads refer to the AdMob Interstitial Ad developer guidelines for apps developed for Android
I have a fragment that contains a listview , I want to show interstial ad whene the user click a listview item , I tried the following code but it is not showing :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
......
mInterstitialAd = new InterstitialAd(mContext);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}
});
......
return rootView;
}
Set up new class which have set, load and show interstatialAd static methods as below:
public class AdSetup {
// Application context returned by function --> Application.get()
public static String TesttingDeviceID = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
final static InterstitialAd mInterstitialAd = new InterstitialAd(Application.get());
public static void setInterstitial() {
try {
if (mInterstitialAd.getAdUnitId() == null) {
mInterstitialAd.setAdUnitId(Application.get().getString(R.string.interstitialAd));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void loadInterstatial() {
try {
setInterstitial();
AdRequest adRequestInter = new AdRequest.Builder().build();
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Log.e("mInterstetial onloaded", "onloaded isLoaded" + mInterstitialAd.isLoaded());
}
#Override
public void onAdFailedToLoad(int i) {
Log.e("mInterstetial onloaded", "onloaded onAdFailedToLoad");
}
#Override
public void onAdOpened() {
// super.onAdOpened();
}
#Override
public void onAdClosed() {
// super.onAdClosed();
}
#Override
public void onAdClicked() {
// super.onAdClicked();
}
});
mInterstitialAd.loadAd(adRequestInter);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void showInterstatial() {
try {
Log.e("mInterstetial show ", "isLoaded" + mInterstitialAd.isLoaded());
if (mInterstitialAd.isLoaded() && mInterstitialAd.getAdUnitId() != null) {
mInterstitialAd.show();
} else {
loadInterstatial();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
How to call this methods on click of the list item or the onCreate() of the new Activity to comply with the interstatial ad policy:
AdSetup.showInterstatial(); // Show ad if already loaded else load Ad
AdSetup.loadInterstatial(); // Load new ad
Make sure to follow the policies listed in the below links:
Disallowed interstitial implementations
App load or exit Do not place interstitial ads on app load and when
exiting apps as interstitials should only be placed in between pages
of app content. Ads should not be placed in applications that are
running in the background of the device or outside of the app
environment. It should be clear to the user which application the ad
is associated with or implemented on.
Recommended interstitial implementations
Also, add Application class:
public class Application extends android.app.Application {
// public static DeviceName.DeviceInfo deviceInfo;
private static Application _instance;
#Override
public void onCreate() {
super.onCreate();
_instance = this;
}
//return the App context
public static Application get() {
return _instance;
}
}
Also declare the Application class in your AndroidManifest.xml as you can see below first line:
<application
android:name=".Application"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:allowBackup="false"
android:supportsRtl="true"
android:networkSecurityConfig="#xml/network_security_config"
android:theme="#style/AppTheme">
I just implemented this in my code. It works. In the example that you gave I did not see if you implemented AdListener after AdRequest.Builder. If not, here is an example of that:
ExampleInterstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
ExampleInterstitial.loadAd(new AdRequest.Builder().build());
}
});
Next, it appeared in your example that you just have the interstitial ad to show onItemClick but do you have an intent to move to a new activity also set there? I didn't see it. Check out my code below. I set the intent to start the new activity then I show the add after. Just a thought because this works for me. Good luck!
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent;
intent = new Intent(getActivity(), ExampleActivity.class);
startActivity(intent);
if (ExampleInterstitial.isLoaded()) {
ExampleInterstitial.show();
}
else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
Im solving this issue for about 3 days. Im implementing Rewarded Ads of Appodeal. When I first start my app It works fine . Appodeal onRewardedVideoLoaded function called and it works fine. But After closing the app and running it again. It always calls onRewardedVideoFailedToLoad function and ads do not load. Below is my code.
Please check this code. Thankyou
/** Set up button to show an ad when clicked */
show_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//ad.show();
if (Appodeal.isLoaded(Appodeal.REWARDED_VIDEO)) {
Appodeal.show(getActivity(), Appodeal.REWARDED_VIDEO);
}
}
});
//appodeal
Appodeal.setAutoCache(Appodeal.REWARDED_VIDEO, false);
Appodeal.initialize(getActivity(), appKey, Appodeal.REWARDED_VIDEO);
// Appodeal.setTesting(true);
Appodeal.setLogLevel(com.appodeal.ads.utils.Log.LogLevel.verbose);
Appodeal.cache(getActivity(), Appodeal.REWARDED_VIDEO);
if (Appodeal.isLoaded(Appodeal.REWARDED_VIDEO)) {
progress.setVisibility(View.INVISIBLE);
System.out.println("Is loaded so enable");
show_button.setText("Earn Reward");
}
Appodeal.setRewardedVideoCallbacks(new RewardedVideoCallbacks() {
private Toast mToast;
#Override
public void onRewardedVideoLoaded() {
System.out.println("onRewardedVideoLoaded");
show_button.setEnabled(true);
progress.setVisibility(View.INVISIBLE);
show_button.setText("Earn Reward");
}
#Override
public void onRewardedVideoFailedToLoad() {
System.out.println("onRewardedVideoFailedToLoad");
progress.setVisibility(View.INVISIBLE);
}
#Override
public void onRewardedVideoShown() {
System.out.println("onRewardedVideoShown");
show_button.setEnabled(false);
show_button.setText("Not Avalible");
progress.setVisibility(View.VISIBLE);
}
#Override
public void onRewardedVideoFinished(int amount, String name) {
System.out.println(String.format("onRewardedVideoFinished. Reward: %d %s", amount, name));
SharedPref.putIntPref("rewards_count", SharedPref.getIntPref("rewards_count", getContext()) + amount, getContext());
// watcher.RemainingRewards(SharedPref.getIntPref("rewards_count", getContext()));
UpdateRewards();
}
#Override
public void onRewardedVideoClosed(boolean finished) {
System.out.println(String.format("onRewardedVideoClosed, finished: %s", finished));
}
void showToast(final String text) {
if (mToast == null) {
mToast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
}
mToast.setText(text);
mToast.setDuration(Toast.LENGTH_SHORT);
mToast.show();
}
});
return view;
}
I had a similar issue, but actually it was connected with framework I used (it turned out after I contacted them), so I'd suggest asking their support directly, usually they respond rather quick + u can message them from LiveChat window on the homepage, right bottom corner. This would be the best way to solve your provlem imo. Not sure that my case will solve your problem, but still it might help you somehow.
i wanted to show skippable and non skippable videos in several projects.
All in all using skippable videos was simple, i only needed to implement GdxAppodeal.VIDEO while initializing.
so to show videos I used GdxAppodeal.getInstance().show(GdxAppodeal.VIDEO);
+
Video callbacks:
GdxAppodeal.getInstance().setVideoCallbacks(new VideoCallback() {
#Override
public void onVideoLoaded() { }
#Override
public void onVideoFailedToLoad() { }
#Override
public void onVideoShown() { }
#Override
public void onVideoFinished() { }
#Override
public void onVideoClosed() { }
});
important nuance was that u can't set reward settings on site for skippable video.
This only works for rev vids.
So, User will get reward on RewardedVideoFinished callback
I'm having a problem with vungle, I want the dialog fragment to appear after the video ad ends, my code looks good and there is no errors, the video starts and ends but my dialog fragment never appears, here is my code:
private final EventListener vungleListener = new EventListener(){
#Override
public void onVideoView(boolean isCompletedView, int watchedMillis, int videoDurationMillis) {
if (isCompletedView){
frag.show(fm, "");
}
}
#Override
public void onAdStart() {
// Called before playing an ad
}
#Override
public void onAdEnd(boolean wasCallToActionClicked) {
}
#Override
public void onAdPlayableChanged(boolean isAdPlayable) {
}
#Override
public void onAdUnavailable(String reason) {
}
};
According to Vungle the method onVideoView is deprecated and the method onAdEnd should be used instead. You should try moving the fragment being shown to onAdEnd like below:
#Override
public void onAdEnd(boolean wasCallToActionClicked) {
frag.show(fm, "");
}
EDIT-----: So I did manage to make it show me users and be able to select some, and to send me back the selected users to my app but there is a slight problem. "Uri.parse("picker://friend");" doesn't give me the list of my friends, but only a list of the friends that I have and have my app installed.
I followed this example: https://developers.facebook.com/docs/android/scrumptious/show-friends but I am trying to modify it, so basically I took out the selectionActivity and Fragment. So I have from my activity a button that calls the PickerActivity which contains FriendPickerFragment. I can select my friends from there, but back in my activities onActivityResult i get back "data" as null.
I have an Application class in my app, where i save the FB session, and also have the login function in there.
In my MainActivity onCreate I have this:
MyApp.getInstance().facebookLogin(PSAddFriendsActivity.this, new CrudStateCallback() {
#Override
public void onResponse(final String string) {
Log.i("", "session : session is opened? : " + MyApp.getInstance().fbSession.getAccessToken());
}
});
After having logged in, I instantiate a list with the current friends I have in my app, and the first position of this list is a FB button:
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
if(position == 0){
startPickerActivity(PSPickerActivity.FRIEND_PICKER, 0);
}else if(position ==1){
//TODO: OPEN CONTACTS PAGE TO ADD FRIENDS
}
}
This is my onACtivityResult and the "startPickerActivity" from this class:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
Log.i("","--------------data is : " + data);
Log.i("","--------------resultCode is : " + resultCode);
Log.i("","--------------requestCode is : " + requestCode);
}
public void startPickerActivity(Uri data, int requestCode) {
Intent intent = new Intent();
intent.setData(data);
intent.setClass(PSAddFriendsActivity.this, PickerActivity.class);
startActivityForResult(intent, requestCode);
}
This is the PickerActivity, how I took it from FB:
public class PickerActivity extends FragmentActivity{
private FriendPickerFragment friendPickerFragment;
public static final Uri FRIEND_PICKER = Uri.parse("picker://friend");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pickers);
Bundle args = getIntent().getExtras();
FragmentManager manager = getSupportFragmentManager();
Fragment fragmentToShow = null;
Uri intentUri = getIntent().getData();
if (FRIEND_PICKER.equals(intentUri)) {
if (savedInstanceState == null) {
friendPickerFragment = new FriendPickerFragment(args);
} else {
friendPickerFragment =
(FriendPickerFragment) manager.findFragmentById(R.id.picker_fragment);
}
// Set the listener to handle errors
friendPickerFragment.setOnErrorListener(new PickerFragment.OnErrorListener() {
#Override
public void onError(PickerFragment<?> fragment,
FacebookException error) {
PSPickerActivity.this.onError(error);
}
});
// Set the listener to handle button clicks
friendPickerFragment.setOnDoneButtonClickedListener(
new PickerFragment.OnDoneButtonClickedListener() {
#Override
public void onDoneButtonClicked(PickerFragment<?> fragment) {
finishActivity();
}
});
fragmentToShow = friendPickerFragment;
} else {
// Nothing to do, finish
setResult(RESULT_CANCELED);
finish();
return;
}
manager.beginTransaction()
.replace(R.id.picker_fragment, fragmentToShow)
.commit();
}
private void onError(Exception error) {
onError(error.getLocalizedMessage(), false);
}
private void onError(String error, final boolean finishActivity) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.error_dialog_title).
setMessage(error).
setPositiveButton(R.string.error_dialog_button_text,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (finishActivity) {
finishActivity();
}
}
});
builder.show();
}
private void finishActivity() {
setResult(RESULT_OK, null);
finish();
}
#Override
protected void onStart() {
super.onStart();
if (FRIEND_PICKER.equals(getIntent().getData())) {
try {
friendPickerFragment.loadData(false);
} catch (Exception ex) {
onError(ex);
}
}
}
#Override
protected void onResume() {
Log.i("", "location test onResume");
super.onResume();
MyApp.getInstance().pref.setIsBackground(this, false);
MyApp.getInstance().startLocationClient();
}
#Override
protected void onPause() {
Log.i("", "location test onPause");
super.onPause();
MyApp.getInstance().pref.setIsBackground(this, true);
}
}
Now I looked over this fragment, do not know if I have to add something or save something from the fragment on "onDoneButtonClicked"? or what exactly, because my main activity does return null as data..
forgot to call this in the finishActivty:
if (FRIEND_PICKER.equals(getIntent().getData())) {
if (friendPickerFragment != null) {
MyApp.getInstance().setSelectedUsers(friendPickerFragment.getSelection());
}
}
Now I can get from my Application the list of selected users.
About my edit, after this i found out that with Graph2.0 you cannot get a list of your whole friendlist. You can only get back the info of friends that also liked the app. It is possible to invite friends to like an app but only if you set it as a Game from the FB developers page