当前位置:操作系统 > 安卓/Android >>

Android程序真退出与假退出后台运行详解

public void onBackPressed() {
  AlertDialog.Builder builder = new Builder(LawdaquanActivity.this);
  builder.setIcon(R.drawable.imagebutton5);
  builder.setTitle("提示");
  builder.setMessage("你真的要退出法律信息软件吗?");
  builder.setPositiveButton("是",
    new android.content.DialogInte易做图ce.OnClickListener() {
     @Override
     public void onClick(DialogInte易做图ce dialog, int which) {
      Intent i = new Intent(Intent.ACTION_MAIN);

      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      i.addCategory(Intent.CATEGORY_HOME);

      startActivity(i);

     }
    });
  builder.setNegativeButton("否",
    new android.content.DialogInte易做图ce.OnClickListener() {
     @Override
     public void onClick(DialogInte易做图ce dialog, int which) {
      dialog.dismiss();
     }
    });
  builder.create().show();

 }

 

-------------真退出
退出应用程序的实现:可以自己写个方法,例如:

public void exitProgrames{

Intent startMain = new Intent(Intent.ACTION_MAIN);

startMain.addCategory(Intent.CATEGORY_HOME);

startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(startMain);

android.os.Process.killProcess(android.os.Process.myPid());

 


--------------重写ONKEYDOWN
public boolean onKeyDown(int keyCode, KeyEvent event) {

  if (keyCode == KeyEvent.KEYCODE_BACK) {
   new AlertDialog.Builder(this)
     .setIcon(R.drawable.title)
     .setTitle("退出软件")
     .setMessage("是否退出法律文书软件")
     .setPositiveButton("否",
       new DialogInte易做图ce.OnClickListener() {

        @Override
        public void onClick(DialogInte易做图ce dialog,
          int which) {
         // TODO Auto-generated method stub

        }
       }).setNegativeButton("退出", new OnClickListener() {

      @Override
      public void onClick(DialogInte易做图ce dialog, int which) {
       // TODO Auto-generated method stub
       finish();
      }
     }).create().show();
  }

  else {
   return super.onKeyDown(keyCode, event);

  }

  // TODO Auto-generated method stub
  return super.onKeyDown(keyCode, event);
 }


-----------假退出
Intent i = new Intent(Intent.ACTION_MAIN);

      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      i.addCategory(Intent.CATEGORY_HOME);

      startActivity(i);
finish();

 

 

摘自 gongzibai的专栏

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,