首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

phonegap在android中怎么退出程序

2013-03-06 
phonegap在android中如何退出程序这个问题有两个解决办法,第一用android在mainActiviy中写一段,第二用phon

phonegap在android中如何退出程序

这个问题有两个解决办法,第一用android在mainActiviy中写一段,第二用phonegap的自带接口


一、核心是方法System.exit(0);,因为finish()啥的都不能用

public class MainActivity extends DroidGap {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setIntegerProperty("loadUrlTimeoutValue", 60000); 
super.loadUrl("file:///android_asset/www/index.html");

}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
promptExit(this);
return true;
}
return super.onKeyDown(keyCode, event);
}
public static void promptExit(final Context con) {
LayoutInflater li = LayoutInflater.from(con);
View exitV = li.inflate(R.layout.exitdialog, null);
AlertDialog.Builder ab = new AlertDialog.Builder(con);
ab.setView(exitV);// 设定对话框显示的View对象
ab.setPositiveButton("退出",
new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
System.exit(0);
}
});
ab.setNegativeButton("取消", null);
// 显示对话框
ab.show();
}

}


二 在js中写

 //添加回退按钮事件
        document.addEventListener("backbutton",onBackKeyDown,false);


        //BackButton按钮
        function onBackKeyDown(){
            if($.mobile.activePage.is('#homepage')){
                navigator.app.exitApp();
            }
            else {
                navigator.app.backHistory();
            }
        }


热点排行