的PhoneGap:2.9.0PhoneGap的後退按鈕:後退和退出
安卓4.4.2
我想用 「後退按鈕」:如果當前頁面不是首頁,返回,否則退出APP,但它不起作用,爲什麼?
這裏是我的代碼:MainActivity.java
import android.os.Bundle;
import android.view.KeyEvent;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import org.apache.cordova.*;
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.splash);
super.loadUrl("http://www.mydomain.com", 0);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && appView.canGoBack())
{
appView.goBack();
return false;
}
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
dialog();
return true;
}
return super.onKeyDown(keyCode, event);
}
protected void dialog() {
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setMessage("Exit The APP?");
builder.setTitle("Are You Sure");
builder.setPositiveButton("OK",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
builder.setNegativeButton("Cancel",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
}
});
builder.create().show();
}
}
感謝您的幫助和良好的祝願!