在应用中肯定遇到有这样的问题,在应用中在于多的Activity中跳转,这些Activity都存在Activity栈中了。所以按返回键的时候都是一个一个的将原来的Activity弹回。如果我们想捕获到back事件之后直接退出整个程序,就要思考了。特别是2.2之后的安全机制考虑之后。
粘贴点代码,以备之后使用。
Java代码
package com.jftt; import Android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestLogout extends Activity { public static final String TAG = TestLogout.class.getSimpleName(); private Button btn1; private Button btn2; private Button btn3; private Button btn4; private Button btn5; private Button go; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logout); this.onStart(); btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { android.os.Process.killProcess(android.os.Process.myPid()); // 获取PID } }); btn2 = (Button) findViewById(R.id.btn2); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.exit(0); // 常规java、c#的标准退出法,返回值为0代表正常退出 } }); btn3 = (Button) findViewById(R.id.btn3); btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "close " + getPackageName()); ActivityManager am = (ActivityManager) TestLogout.this .getSystemService(Context.ACTIVITY_SERVICE); am.restartPackage(getPackageName()); // am.killBackgroundProcesses(getPackageName()); } }); btn4 = (Button) findViewById(R.id.btn4); btn4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); // intent.setClass((B或者C或者D).this, A.class); intent.setClass(TestLogout.this, TestLogout.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("flag", 1); startActivity(intent); } }); //此方法并未杀掉应用程序 而是把launcher调起 btn5 = (Button) findViewById(R.id.btn5); btn5.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain); } }); go = (Button) findViewById(R.id.go); go.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(TestLogout.this, TestLogout.class); // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }); } protected void onStart() { super.onStart(); Intent intent = getIntent(); int x = intent.getIntExtra("flag", -1); if (x == 0) { finish(); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { AlertDialog.Builder alertbBuilder = new AlertDialog.Builder(this); alertbBuilder.setIcon(R.drawable.icon).setTitle("友情提示...").setMessage("你确定要离开吗?"); alertbBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 结束这个Activity int nPid = android.os.Process.myPid(); android.os.Process.killProcess(nPid); } }); alertbBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).create(); alertbBuilder.show(); } return true; } }