gpt4 book ai didi

android - 按两次返回退出

转载 作者:行者123 更新时间:2023-11-29 19:15:02 24 4
gpt4 key购买 nike

Android 网页 View 我的应用程序中的当前代码是这样工作的:当用户按一次后退时,它会显示按钮询问“你想退出吗?”显示是或否选项。当我们选择是时,它会退出应用程序并显示插页式广告。如果您按“否”,它将保留在 Activity 中。

我想要的是:当用户按下后退时,它将转到上一个 Activity 。如果用户双击后退按钮然后它会要求退出,如果用户选择是。用户将退出应用程序并出现插页式广告。

请帮我解决这个问题。

主要 Activity :

public class MainActivity extends Activity{

private Fragment contentFragment;
String testDevice = "D0A04359EA1ECE9BA0CD4B6F457A9991";
String testDevice2 = "63C3530DA03C191310DB9AB8F0672E5C";
String testDevice3 = "801F2141A1DC3F743363AFDFDC42AF3A";
private InterstitialAd mInterstitialAd;
private AdView mAdView;
boolean displayAd = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl(this.getString(R.string.channel_url));

mAdView = (AdView) findViewById(R.id.ad_view);
// Create an ad request. Check your logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(testDevice)
.addTestDevice(testDevice2)
.addTestDevice(testDevice3)
.build();

mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
displayAd = true;
// View servername = findViewById(R.id.txt_List);
// RelativeLayout.LayoutParams layoutparams =
(RelativeLayout.LayoutParams) servername.getLayoutParams();
// layoutparams.addRule(RelativeLayout.BELOW, mAdView.getId());
// layoutparams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
// servername.setLayoutParams(layoutparams);
}

@Override
public void onAdFailedToLoad(int errorCode) {
if (!displayAd) {
}
}

@Override
public void onAdClosed() {
// Proceed to the next level.
}
});

// Start loading the ad in the background.
mAdView.loadAd(adRequest);

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    // Create the InterstitialAd and set the adUnitId (defined in values/strings.xml).
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}

private class MyCustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

private InterstitialAd newInterstitialAd() {
InterstitialAd interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
}

@Override
public void onAdFailedToLoad(int errorCode) {
}

@Override
public void onAdClosed() {
// Proceed to the next level.
finish();
//goToNextLevel();
}
});
return interstitialAd;
}

private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and reload the ad.
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();

} else {
finish();
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

private void loadInterstitial() {
// Disable the next level button and load the ad.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(testDevice)
.addTestDevice(testDevice2)
.addTestDevice(testDevice3)
.setRequestAgent("android_studio:ad_template").build();
mInterstitialAd.loadAd(adRequest);
}


/*
* We call super.onBackPressed(); when the stack entry count is > 0. if it
* is instanceof EmpListFragment or if the stack entry count is == 0, then
* we prompt the user whether to quit the app or not by displaying dialog.
* In other words, from EmpListFragment on back press it quits the app.
*/

@Override
public void onBackPressed() {
onShowQuitDialog();
}

public void onShowQuitDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);

builder.setMessage("Do You Want To Quit?");
builder.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
showInterstitial();
}
});
builder.setNegativeButton(android.R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create().show();
}

最佳答案

这是最简单的代码:

private static final int TIME_DELAY = 2000;
private static long back_pressed;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public void onBackPressed() {
if (back_pressed + TIME_DELAY > System.currentTimeMillis()) {
super.onBackPressed();
} else {
Toast.makeText(getBaseContext(), "Press once again to exit!",
Toast.LENGTH_SHORT).show();
}
back_pressed = System.currentTimeMillis();
}
}

关于android - 按两次返回退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43847825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com