Unexplained delay between ad onAdClosed and finish - delay

After clicking back button, dialog is opened asking if user want to save his work before returning to menu activity (finishing current activity).
If there is ad loaded, it will show and the activity is finished on onAdClosed.
But there is unexplained delay between closing ad and returning to the menu. If the ad is displayed for some time, the delay disappear. When the ad is not loaded so it will not be shown, finishing is smooth too.
mInterstitialBack.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
finish();
}
});
builderSingle.setNegativeButton(getString(R.string.noSave),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (mInterstitialBack.isLoaded()) {
mInterstitialBack.show();
} else {
finish();
}
}
});

Related

Android Studio: How to create particular Alert Dialog based on where the user clicks

I am new to Android Studio programming. My program has a recyclerview with 3 Textviews, and one image. I would like an alert dialog to appear based on where the user clicks on the screen, for instance if the user clicks on the image the dialog says, "You clicked on image". If the user clicks on the text, it would say "You clicked on the text".
My onclick listener is as follows: itemView.setOnClickListener(this);
#Override
public void onClick(View view) {
int index = getAdapterPosition();
if (playerNames.equals(index)) {
showDialogPlayerNames();
}
if (numberPlayers.equals(index)){
showDialogPlayerNumber();
}
if (ScorePlayers.equals(index)){
showDialogPlayerScore();
}
if (picturePlayers.equals(index)) {
showDialogPlayerImage();
}
}
}
Here is my alert dialog code:
void showDialogPlayerNames() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Click Information");
alertDialog.setIcon(R.drawable.ic_baseline_info_24);
alertDialog.setMessage("You clicked Player Name");
alertDialog.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.create().show();
}
Thank you
You need to add parameters in your methods
showDialogPlayerNames("Something text here");
void showDialogPlayerNames(String myText) {
alertDialog.setMessage(myText);
}

Show interstitialAd after 30 seconds Audience Network Facebook

I want to use Facebook Audience Network for my Android app, I have integrated interstitialAd in-app and it works fine but it appears as Activity launch. I want to show ad after 30 seconds. this is my code
interstitialAd = new InterstitialAd(this, "1555910157949688_1556058954601475");
interstitialAd.setAdListener(new InterstitialAdListener() {
#Override
public void onAdLoaded(Ad ad) {
// Interstitial ad is loaded and ready to be displayed
Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
// Show the ad
interstitialAd.show();
}
});
interstitialAd.loadAd();
and I have tried this to schedule my ad but it did not work.
private void showAdWithDelay() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Check if interstitialAd has been loaded successfully
if(interstitialAd == null || !interstitialAd.isAdLoaded()) {
return;
}
// Check if the ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad.
if(interstitialAd.isAdInvalidated()) {
return;
}
// Show the ad
interstitialAd.show();
}
}, 3000);
}
I prefer to do this:
Handler handler = new Handler();
interstitialAd = new InterstitialAd(this, "XXX");
interstitialAd.setAdListener(new InterstitialAdListener() {
#Override
public void onAdLoaded(Ad ad) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
interstitialAd.show();
}
}, YOUR_TIME);
}
});
interstitialAd.loadAd();
And if you want to be more precise, do it:
final long lastTime = System.currentTimeMillis();
Handler handler = new Handler();
interstitialAd = new InterstitialAd(this, "XXX");
interstitialAd.setAdListener(new InterstitialAdListener() {
#Override
public void onAdLoaded(Ad ad) {
long now = System.currentTimeMillis();
long timeWait;
if (now - lastTime >= YOUR_TIME){
timeWait = 0;
}else {
timeWait = YOUR_TIME-(now-lastTime);
}
handler.postDelayed(new Runnable() {
#Override
public void run() {
interstitialAd.show();
}
}, timeWait);
}
});
interstitialAd.loadAd();
I think your implementation is breaking AdMob policies and could get you some trouble.
Check this page for help on right/wrong Interstitial implementations (Specially the "Interstitials that unexpectedly launch" point): https://support.google.com/admob/answer/6201362?hl=en&ref_topic=2936214
To comply with AdMob policies I suggest you only show Interstitials in activity/app natural transitions, different implementations could lead you to get banned from Admob.
To do so, you could change your code to first just load the Ad and keep track of its loaded status like this:
boolean isAdLoaded = false;
interstitialAd = new InterstitialAd(this, "1555910157949688_1556058954601475");
interstitialAd.setAdListener(new InterstitialAdListener()
{
#Override
public void onAdLoaded(Ad ad)
{
// Interstitial ad is loaded and ready to be displayed
Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
// The ad has been loaded
isAdLoaded = true;
}
});
interstitialAd.loadAd();
And then show it in a natural transition of the app, for example when the user taps a button to go to a different activity:
shareButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Show interstitial if it is loaded
if(isAdLoaded)
interstitialAd.show();
//Open Activity
startActivity(new Intent(this,OtherActivity.class));
}
});

How to turn off an app with a button in android

i try to implement a way to turn off an app with a button. If i push the button, the app seems to being closed, but if i look which applications a currently running, my app is still active.
Here ist my code:
turnOffApp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finishAffinity();
System.exit(0);
}
});

Android Studio - AlertDialog button listener, and view of ProgressBar

This is the code to test the thread and how it works! (It's not really connecting just to look like one) I wanted to add a progress bar when the alert dialog is gone by pressing the yes button, but it doesn't work. I set the Visibility to gone before, and visible to after. What seems to be the problem? I done casting right, and there's nothing wrong elsewhere. The text doesn't change also..... please help :)
private void connectRequest() {
AlertDialog.Builder builder = new AlertDialog.Builder(ThreadActivity2.this);
AlertDialog dialog = builder.setMessage("Do you request Remote Access?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
accessTv.setText("Requesting Remote Access ...");
accessPgb.setVisibility(View.VISIBLE);
try {
Thread.sleep(5700);
} catch (InterruptedException e) {
e.printStackTrace();
}
accessTv.setText("Server Access Successful!");
accessPgb.setVisibility(View.GONE);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
Try using java.util.concurrent.TimeUnit:
TimeUnit.SECONDS.sleep(1);
To sleep for one second or
TimeUnit.MINUTES.sleep(1);

Android show dialog in Custom View

I create a custom view which showing the simple game
I set the custom view in the MainActivity
setContentView(new CustomView())
In this custom view, only have few ball and the timer
When a ball touch another ball. The timer will stop and show the result.
I don't know how to show the result in a better way. So i tried to create a dialog to show the result.
This code is write in the CustomView class
if (ballIsTouch) {
AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
alertDialog.setTitle("Result");
alertDialog.setMessage(point);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
However, the page is freezed. The dialog haven't show.
create() - is a final step -when you use builder pattern to create dialog u first set in chain all variables and then create dialog - and show so it should be
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
alertDialogBuilder.setTitle("Result");
alertDialogBuilder.setMessage(point);
alertDialogBuilder.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
or
alertDialogBuilder.setTitle("Result")
.setMessage(point)
.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ensure you getContext() is valid (should be activity, service context)**
in activity use this or Myactivity.this in interface/callback in fragment getActivity()
**see Dialog creation context

Resources