在应用中肯定遇到有这样的问题,在应用中在于多的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;
- }
- }
Java代码
- package com.jftt;
- import java.util.Stack;
- import Android.app.Activity;
- public class ActiivtyStack {
- private static Stack<Activity> mActivityStack;
- private static ActiivtyStack instance;
- private ActiivtyStack() {
- }
- public static ActiivtyStack getScreenManager() {
- if (instance == null) {
- instance = new ActiivtyStack();
- }
- return instance;
- }
- // 退出栈顶Activity
- public void popActivity(Activity activity) {
- if (activity != null) {
- activity.finish();
- mActivityStack.remove(activity);
- // mActivityStack.pop();
- activity = null;
- }
- }
- // 获得当前栈顶Activity
- public Activity currentActivity() {
- Activity activity = mActivityStack.lastElement();
- // Activity activity = mActivityStack.pop();
- return activity;
- }
- // 将当前Activity推入栈中
- public void pushActivity(Activity activity) {
- if (mActivityStack == null) {
- mActivityStack = new Stack<Activity>();
- }
- mActivityStack.add(activity);
- // mActivityStack.push(activity);
- }
- // 退出栈中所有Activity
- public void popAllActivityExceptOne(Class<Activity> cls) {
- while (true) {
- Activity activity = currentActivity();
- if (activity == null) {
- break;
- }
- if (activity.getClass().equals(cls)) {
- break;
- }
- popActivity(activity);
- }
- }
- }
logout.xml
Xml代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/btn1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="logout button1"
- />
- <Button
- android:id="@+id/btn2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="logout button2"
- />
- <Button
- android:id="@+id/btn3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="logout button3"
- />
- <Button
- android:id="@+id/btn4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="go to first"
- />
- <Button
- android:id="@+id/btn5"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="go to launcher"
- />
- <Button
- android:id="@+id/go"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="go another activity"
- />
- <!--
- <EditText
- android:id="@+id/et01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- <ImageView
- android:id="@+id/iv01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- -->
- </LinearLayout>
manifest中的权限:
Xml代码
- <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
-
<uses-permission android:name="android.permission.RESTART_PACKAGE" />