AdListner not working properly - android

I wish to remove the ad if they are clicked once.
I implemented the following code.
I also added android:launchMode="singleInstance"
in the manifest and android:alwaysRetainTaskState="true" in the activity section of the manifest.
But when i click on the ad and return to the app the ad is still showing .
My code is as follows.
public class MainActivity extends Activity implements AdListener{
adView = (AdView)findViewById(R.id.ad);
adView.loadAd(new AdRequest());
// my code
#Override
public void onDismissScreen(Ad arg0) {
if (adView != null) {
adView.destroy();
}
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
}

adView.setAdListener(this); /* first you should add listener register */
and for dismiss screen
public void onDismissScreen(Ad p1)
{
yourlayout.removeView(adView); // your layout should me remove here
}

Related

Admob interstitial ads are not closing

I'm showing interstitial ad in my app and ads are loading and displaying perfectly but the issue is that ads are not closing when I click cross button on back button I have to quit my app to close them
I created a new project and checked the ads in new project they are working fine and closing on back button but in my main app ads are not closing once they displayed to user
this is my Admob class
public class Admob {
public static InterstitialAd mInterstitial; // Interstital
private static AdView mAdView; // banner
public static void createLoadInterstitial(final Context context, View view)
{
mInterstitial = new InterstitialAd(context);
mInterstitial.setAdUnitId(context.getResources().getString(
R.string.admob_showIntersitial_ad_unit_id));
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
showInterstitial();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
// Called when an ad leaves the app (for example, to go to the
// browser).
super.onAdLeftApplication();
}
});
loadInterstitial();
}
public static void loadInterstitial() {
mInterstitial.loadAd(new AdRequest.Builder().
addTestDevice("").//ca-app-pub-3940256099942544/1033173712
build());
}
public static void showInterstitial() {
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
}
public static void createLoadBanner(final Context context, View view) {
mAdView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().
addTestDevice("").//ca-app-pub-3940256099942544/6300978111
build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
}
});
}
}
This is how I show them in my activity
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
Admob.createLoadInterstitial(this,null);
I have initialize this in Mainfest also
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
I saw same questions on stackoverflow but none of them is answered the solution so any help will be really appreciated
If you are using Webiew check if there is webview is paused on ad shown

Web View running on other than UI thread

I am trying to show flurry interstitial but getting following message in debug screen and I am not receiving interstitial on my screen.
07-14 15:55:31.390: W/webview(10588): java.lang.Throwable: Warning: A
WebView method was called on thread 'FlurryAgent'. All WebView methods
must be called on the UI thread. Future versions of WebView may not
support use on other threads.
I have followed this tutorial completely :
Android Integration
At present I am working on AndEngine. As error replying I put my all code in UI thread but result is same.
Here is my code for displaying ads :
protected void onCreate(Bundle pSavedInstanceState) {
super.onCreate(pSavedInstanceState);
// configure Flurry
FlurryAgent.setLogEnabled(false);
// init Flurry
FlurryAgent.init(MainGameActivity.this, MY_FLURRY_APIKEY);
mFlurryAdInterstitial = new FlurryAdInterstitial(MainGameActivity.this,
MY_ADSPACE_NAME);
FlurryAdTargeting adTargeting = new FlurryAdTargeting();
// enable test mode for this interstitial ad unit
adTargeting.setEnableTestAds(true);
mFlurryAdInterstitial.setTargeting(adTargeting);
// allow us to get callbacks for ad events
mFlurryAdInterstitial.setListener(interstitialAdListener);
mFlurryAdInterstitial.fetchAd();
}
FlurryAdInterstitialListener interstitialAdListener = new FlurryAdInterstitialListener() {
#Override
public void onFetched(final FlurryAdInterstitial adInterstitial) {
adInterstitial.displayAd();
}
#Override
public void onError(final FlurryAdInterstitial adInterstitial,
FlurryAdErrorType adErrorType, int errorCode) {
adInterstitial.destroy();
}
#Override
public void onAppExit(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onClicked(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onClose(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onDisplay(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onRendered(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onVideoCompleted(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
};
#Override
protected void onStart() {
super.onStart();
FlurryAgent.onStartSession(MainGameActivity.this);
}
#Override
protected void onStop() {
super.onStop();
FlurryAgent.onEndSession(MainGameActivity.this);
}
#Override
public void onDestroy() {
super.onDestroy();
mFlurryAdInterstitial.destroy();
}
So what can I do in this situation?

how to call ad mob ads show in some time intervals or different Activity in android apps

I am use ads of ad mob for advertise in my application its always display on on start up of my application but I want display this ads on when user exit application and back to the main activity call so how it implement in my app
my code is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interstitial = new InterstitialAd(this, "a152eb628a493d8");
adRequest = new AdRequest();
interstitial.loadAd(adRequest);
interstitial.setAdListener(this);
web=(WebView)findViewById(R.id.webview);
web.getSettings().setJavaScriptEnabled(true);
web.setWebViewClient(new WebViewClient());
web.getSettings().setBuiltInZoomControls(true);
web.loadUrl("http://dcs-dof.gujarat.gov.in/live-info.htm");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1,Menu.NONE,"About");
menu.add(0,2,Menu.NONE,"Feedback");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id == 1)
{
Toast.makeText(MainActivity2.this,"About",Toast.LENGTH_LONG).show();
Intent i=new Intent(MainActivity2.this,about.class);
startActivity(i);
}
else {
Toast.makeText(MainActivity2.this,"Feedback",Toast.LENGTH_LONG).show();
Intent i2 =new Intent(MainActivity2.this,feedback.class);
startActivity(i2);
}
return super.onOptionsItemSelected(item);
}
private boolean doubleBackToExitPressedOnce = false;
#Override
protected void onResume() {
//interstitial.loadAd(adRequest);
super.onResume();
this.doubleBackToExitPressedOnce = false;
}
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this,"Press Again to Exit", Toast.LENGTH_SHORT).show();
}
#Override
public void onDismissScreen(Ad ad) {
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onRestart();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onStart();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
//interstitial.loadAd(adRequest);
super.onDestroy();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
//interstitial.loadAd(adRequest);
super.onStop();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onPause();
}
You really don't want to attempt to display an ad to a user on exit from your Actvity, it is a poor user experience.
Even if you did orchestrate a way to get the ad and present prior to your Activity's destruction you don't know how long it will take to retrieve the ad so your Activity might hang for several seconds on exit. User's hate that kind of thing.
I strongly suggest you find another spot within your app in which to display ads.

How to keep playing YouTubePlayer in Android

Now I'm creating an Android application witch use YouTube api to play Video.
But the player stop when I change Activity.
I want to keep Playing like a Music Player even when Activity will change.
I try to solve that by adding Static on YouTubePlayer.(But cannot solve.)
I mean I want to Keep it's playing when the Activity or the Application itself move to Background.
So if you have any ideas, please share with me :)
-----------------Code-----------------
public class PlayActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,YouTubePlayer.PlayerStateChangeListener{
public static final String DEVELOPER_KEY = "My_Dev_Key";
private LinearLayout yPos;
private static YouTubePlayer yt;
private YouTubePlayerView ytp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
yPos = (LinearLayout)findViewById(R.id.youtube_pos);
try{
ytp = new YouTubePlayerView(this);
ytp.initialize(DEVELOPER_KEY, this);
}catch(Exception e){
finish();
}
yPos.addView(ytp);
}
#Override
public void onAdStarted() {
// TODO Auto-generated method stub
}
#Override
public void onError(ErrorReason arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLoaded(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLoading() {
// TODO Auto-generated method stub
}
#Override
public void onVideoEnded() {
// TODO Auto-generated method stub
}
#Override
public void onVideoStarted() {
// TODO Auto-generated method stub
}
#Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
#Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
boolean arg2) {
// TODO Auto-generated method stub
if(!arg2){
if(yt == null){
yt = arg1;
yt.setPlayerStateChangeListener(this);
yt.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
yt.loadVideo("u0v5A6cQOhs");
}
}
}
}

Android - AdMob Set Ads to GONE?

I wanted to ask, how to implement an AdListner for Admob. I want the Ad to disapear if its clicked.
I tried this but it did not help.
final AdView ad1 = (AdView) findViewById(R.id.ad1);
ad1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ad1.setVisibility(View.GONE);
ad1.destroy(); }
});
thank you.
Depending on the device you are on, you may need to use that View's invalidate() method. (I would notice I needed to do this for some ad stuff on freewheel on 2.1 devices).
-- OR --
Try placing your ad1 view in a wrapper view (LinearLayout or something small) and then setting that wrapper view's visibility to View.GONE.
I think as AdView is RelativeLayout so you need to
* ad1.setClickable(true);
or
* can put entry in layout XML `android:clickable="true"`
If someone is still searching for this, here is the code I used:
public class myActivity extends Activity implements AdListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView adView = (AdView)this.findViewById(R.id.ads2);
adView.setAdListener(this);
adView.loadAd(new AdRequest());
}
public void onDismissScreen(Ad arg0) {
RelativeLayout rellayout = (RelativeLayout) findViewById(R.id.rellayout);
AdView adView = (AdView)this.findViewById(R.id.ads2);
rellayout.removeView(adView);
}
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
}

Categories

Resources