2017-05-08 187 views
1

Android Webview 當前代碼在我的應用程序中是這樣工作的: 當用戶按下單次時,它會顯示按鈕詢問「你想要退出嗎?顯示是或否選項。當我們選擇「是」時,它會退出應用並顯示插頁式廣告。如果按否,它將保留在活動中。按雙重返回退出

我想要的是: 當用戶按回它將會去上一個活動。如果用戶雙擊返回按鈕,則它將要求退出並且如果用戶選擇是。用戶將退出應用並顯示插頁式廣告。

請幫我解決這個問題。 。

主要活動:

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("ABCDEF") 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(); 
} 

}

+0

不幸的是,沒有'GestureDetector'爲返回鍵,但一個快速的方法是,當用戶按下後退按鈕開始計時,如果有第二敲擊前定時器耗盡,那麼你認爲這是一個雙擊。否則,這是一個簡單的點擊,你可以完成你的活動。 –

+0

我已經更新了上面的整個代碼。請檢查並建議相應的更改。所以它對於插頁式廣告也可以很好地工作。 –

+0

在您的單一背部按鍵上打電話給您的上一個活動,並退出雙後背按,因爲我在回答中提到請檢查https:// stackoverflow。com/questions/43847825/press-double-back-to-exit/46639761#46639761 –

回答

0
boolean doubleBackToExitPressedOnce = false; 

@Override 
    public void onBackPressed() { 
     if (doubleBackToExitPressedOnce) { 
      super.onBackPressed(); 
      return; 
     } 

     this.doubleBackToExitPressedOnce = true; 

     onShowQuitDialog(); 


     new Handler().postDelayed(new Runnable() { 

      @Override 
      public void run() { 
       doubleBackToExitPressedOnce=false; 
      } 
     }, 2000); 
    } 
+0

它的顯示無法解析符號處理程序。由於文本限制,無法粘貼整個主要活動代碼。您沒有包含插頁式廣告。請檢查並給我廣告代碼。 –

+0

請在此檢查整個主要活動代碼:https://drive.google.com/file/d/0B6a1Wje6UpUXNDR0YVFfMkpyUTg/view?usp=sharing –

+0

我已經更新了上面的整個代碼。請檢查並建議相應的更改。所以它對於插頁式廣告也可以很好地工作。 –

0

嘗試下面的代碼:

int backFlag = 0; 

@Override 
public void onBackPressed() { 

backFlag ++; 

if(backflag == 1){ 
super.onBackPressed(); 
    // go to previous activity 
} 
else if(backFlag == 2){ 
    // ask user to exit 
} 

new Handler().postDelayed(new Runnable() { 

    @Override 
    public void run() { 
     backFlag=0;      
    } 
}, 2000); 
} 
+0

它的顯示無法解析符號處理程序。由於文本限制,我無法粘貼整個主要活動代碼。您沒有包含插頁式廣告。請檢查並給我廣告代碼。 –

+0

你必須爲它導入Handler類。 –

+0

請在此檢查整個主要活動代碼:https://drive.google.com/file/d/0B6a1Wje6UpUXNDR0YVFfMkpyUTg/view?usp=sharing –

0

一個臨時標誌將做的事。 將此代碼添加到onBackPressed()方法中。

boolean isSecond; 

@Override 
public void onBackPressed(){ 
    if (isSecond) { 
     // Open your dialog here 
    } 

    isSecond = true; 
    new Handler() . postDelayed(new Runnable() { 
     @Override 
     public void run(){ 
      isSecond = false; 
     } 
    }, 2000); 
} 
+0

它的顯示無法解析符號處理程序。由於文本限制,我無法粘貼整個主要活動代碼。您沒有包含插頁式廣告。請檢查並給我廣告代碼。 –

+0

請在此檢查整個主要活動代碼:https://drive.google.com/file/d/0B6a1Wje6UpUXNDR0YVFfMkpyUTg/view?usp=sharing –

+0

我已經更新了上面的整個代碼。請檢查並建議相應的更改。所以它對於插頁式廣告也可以很好地工作。 –

0

有雙onBackPressed()沒有內置的功能,所以你必須構建你自己的版本。 Android系統提供了一個Handler,您可以撥打postDelayed來測量兩次點擊之間的時間。

例如這樣

boolean backPressed = false; 
boolean backPressedTwice = false; 
@Override 
public void onBackPressed() { 
    if(!backPressed){ 
     backPressed = true; 
     new Handler().postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      if(!backPressedTwice) 
      MainActivity.this.finish(); // go back to previous activity      
     } 
    }, 500); 
} 
if(backPressed) { 
    // it's clicked twice 
    backPressedTwice = true; // cancel the handler so it does not finish the activity 
    onShowQuitDialog(); 
    } 
} 
+0

它的顯示無法解析符號處理程序。由於文本限制,無法粘貼整個主活動代碼。您沒有包含插頁式廣告。請檢查並給我廣告代碼。 –

+0

請在此檢查整個主要活動代碼:https://drive.google.com/file/d/0B6a1Wje6UpUXNDR0YVFfMkpyUTg/view?usp=sharing –

+0

我已經更新了上面的整個代碼。請檢查並建議相應的更改。所以它對於插頁式廣告也可以很好地工作。 –

0

演示您的任務:

public class MainActivity extends AppCompatActivity { 

    Handler handler = new Handler(); 
    boolean ShowDialog = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     handler = new Handler(getMainLooper()); 
    } 


    @Override 
    public void onBackPressed() { 
     if (!ShowDialog) { 
      this.ShowDialog = true; 
      handler.postDelayed(runnable, 2000);// time setting .. current is 2 sec 
     } else { 
      handler.removeCallbacks(runnable); 
      ShowDialog = false; 
      ShowDailog(); 
     } 
    } 

    private void ShowDailog() { 
     Toast.makeText(this, "show dialog here", Toast.LENGTH_LONG).show(); 
    } 

    Runnable runnable = new Runnable() { 
     @Override 
     public void run() { 
      ShowDialog = false; 
      MainActivity.super.onBackPressed(); 
     } 
    }; 
} 
+0

它的顯示無法解析符號處理程序。由於文本限制,我無法粘貼整個主活動代碼。您沒有包含插頁式廣告。請檢查並給我廣告代碼。 –

+0

請在此檢查整個主要活動代碼:https://drive.google.com/file/d/0B6a1Wje6UpUXNDR0YVFfMkpyUTg/view?usp=sharing –

+0

我已經更新了上面的整個代碼。請檢查並建議相應的更改。所以它對於插頁式廣告也可以很好地工作。 –

0

你可以做這樣的事情

private long backPressedTime = 0; 
private long backPressThreshold = 400; 
private Handler mHandler = new Handler(); 
private Runnable bacPressedRunnable = new Runnable() { 
    @Override 
    public void run() { 
     MYActivity.super.onBackPressed(); 
    } 
}; 

@Override 
public void onBackPressed() { 
    if(backPressedTime = 0){ 
     backPressedTime = Systemm.currentTimeInMillis(); 
     mHandler.postDelayed(bacPressedRunnable,backPressThreshold); 
    }else if(System.currentTimeInMillis()-backPressedTime >= backPressThreshold){ 
     mHandler.removeMessages(bacPressedRunnable); 
     onShowQuitDialog(); 
    }else{ 
     super.onBackPressed(); 
    } 
    backPressedTime = 0; 
} 
+0

它的顯示無法解析符號處理程序。由於文本限制,我無法粘貼整個主要活動代碼。您沒有包含插頁式廣告。請檢查並給我廣告代碼。 –

+0

請在此檢查整個主要活動代碼:https://drive.google.com/file/d/0B6a1Wje6UpUXNDR0YVFfMkpyUTg/view?usp=sharing –

+0

我已經更新了上面的整個代碼。請檢查並建議相應的更改。所以它對於插頁式廣告也可以很好地工作。 –

0

這可能是最簡單的代碼:

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(); 
    } 
} 
0

退出android應用程序雙後退按下使用以下更新的代碼。它可能會解決你的問題

private boolean doubleBackToExitPressedOnce; 
private void onApplicationBackPressed() { 
    if (doubleBackToExit) { 
     moveTaskToBack(true); 
     android.os.Process.killProcess(android.os.Process.myPid()); 
     System.exit(1); 
     return; 
    } 

    doubleBackToExit= true; 

    Utilities_dialer.showToastMessage(this, "Press Again To Exit"); 
    //use your dialog box or toast message 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      doubleBackToExit= false; 
     } 
    }, 2000); 
}