Android 应用程序多Activity跳转之后退出整个程序
在应用中肯定遇到有这样的问题,在应用中在于多的Activity中跳转,这些Activity都存在Activity栈中了。所以按返回键的时候都是一个一个的将原来的Activity弹回。如果我们想捕获到back事件之后直接退出整个程序,就要思考了。特别是2.2之后的安全机制考虑之后。
粘贴点代码,以备之后使用。
Java代码- 1.package com.jftt;
- 2.
- 3.import android.app.Activity;
- 4.import android.app.ActivityManager;
- 5.import android.app.AlertDialog;
- 6.import android.content.Context;
- 7.import android.content.DialogInterface;
- 8.import android.content.Intent;
- 9.import android.os.Bundle;
- 10.import android.util.Log;
- 11.import android.view.KeyEvent;
- 12.import android.view.View;
- 13.import android.view.View.OnClickListener;
- 14.import android.widget.Button;
- 15.
- 16.public class TestLogout extends Activity {
- 17. public static final String TAG = TestLogout.class.getSimpleName();
- 18. private Button btn1;
- 19. private Button btn2;
- 20. private Button btn3;
- 21. private Button btn4;
- 22. private Button btn5;
- 23. private Button go;
- 24.
- 25. @Override
- 26. protected void onCreate(Bundle savedInstanceState) {
- 27. super.onCreate(savedInstanceState);
- 28. setContentView(R.layout.logout);
- 29. this.onStart();
- 30.
- 31. btn1 = (Button) findViewById(R.id.btn1);
- 32. btn1.setOnClickListener(new OnClickListener() {
- 33. @Override
- 34. public void onClick(View v) {
- 35. android.os.Process.killProcess(android.os.Process.myPid()); // 获取PID
- 36. }
- 37. });
- 38.
- 39. btn2 = (Button) findViewById(R.id.btn2);
- 40. btn2.setOnClickListener(new OnClickListener() {
- 41. @Override
- 42. public void onClick(View v) {
- 43. System.exit(0); // 常规java、c#的标准退出法,返回值为0代表正常退出
- 44. }
- 45. });
- 46.
- 47. btn3 = (Button) findViewById(R.id.btn3);
- 48. btn3.setOnClickListener(new OnClickListener() {
- 49. @Override
- 50. public void onClick(View v) {
- 51. Log.i(TAG, "close " + getPackageName());
- 52. ActivityManager am = (ActivityManager) TestLogout.this .getSystemService(Context.ACTIVITY_SERVICE);
- 53. am.restartPackage(getPackageName());
- 54. // am.killBackgroundProcesses(getPackageName());
- 55. }
- 56. });
- 57.
- 58. btn4 = (Button) findViewById(R.id.btn4);
- 59. btn4.setOnClickListener(new OnClickListener() {
- 60. @Override
- 61. public void onClick(View v) {
- 62. Intent intent = new Intent();
- 63. // intent.setClass((B或者C或者D).this, A.class);
- 64. intent.setClass(TestLogout.this, TestLogout.class);
- 65. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- 66. intent.putExtra("flag", 1);
- 67. startActivity(intent);
- 68. }
- 69. });
- 70.
- 71. //此方法并未杀掉应用程序 而是把launcher调起
- 72. btn5 = (Button) findViewById(R.id.btn5);
- 73. btn5.setOnClickListener(new OnClickListener() {
- 74. @Override
- 75. public void onClick(View v) {
- 76. Intent startMain = new Intent(Intent.ACTION_MAIN);
- 77. startMain.addCategory(Intent.CATEGORY_HOME);
- 78. startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- 79. startActivity(startMain);
- 80. }
- 81. });
- 82.
- 83. go = (Button) findViewById(R.id.go);
- 84. go.setOnClickListener(new OnClickListener() {
- 85. @Override
- 86. public void onClick(View v) {
- 87. Intent intent = new Intent(TestLogout.this, TestLogout.class);
- 88. // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- 89. startActivity(intent);
- 90. }
- 91. });
- 92.
- 93. }
- 94.
- 95. protected void onStart() {
- 96. super.onStart();
- 97. Intent intent = getIntent();
- 98. int x = intent.getIntExtra("flag", -1);
- 99. if (x == 0) {
- 100. finish();
- 101. }
- 102. }
- 103.
- 104. @Override
- 105. public boolean onKeyDown(int keyCode, KeyEvent event) {
- 106. if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
- 107. AlertDialog.Builder alertbBuilder = new AlertDialog.Builder(this);
- 108. alertbBuilder.setIcon(R.drawable.icon).setTitle("友情提示...").setMessage("你确定要离开吗?");
- 109. alertbBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- 110. @Override
- 111. public void onClick(DialogInterface dialog, int which) {
- 112. // 结束这个Activity
- 113. int nPid = android.os.Process.myPid();
- 114. android.os.Process.killProcess(nPid);
- 115. }
- 116. });
- 117. alertbBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- 118. @Override
- 119. public void onClick(DialogInterface dialog, int which) {
- 120. dialog.cancel();
- 121. }
- 122. }).create();
- 123. alertbBuilder.show();
- 124. }
- 125. return true;
- 126. }
- 127.}
-
复制代码 Java代码- 1.package com.jftt;
- 2.
- 3.import java.util.Stack;
- 4.
- 5.import android.app.Activity;
- 6.
- 7.public class ActiivtyStack {
- 8. private static Stack<Activity> mActivityStack;
- 9. private static ActiivtyStack instance;
- 10.
- 11. private ActiivtyStack() {
- 12. }
- 13. public static ActiivtyStack getScreenManager() {
- 14. if (instance == null) {
- 15. instance = new ActiivtyStack();
- 16. }
- 17. return instance;
- 18. }
- 19.
- 20. // 退出栈顶Activity
- 21. public void popActivity(Activity activity) {
- 22. if (activity != null) {
- 23. activity.finish();
- 24. mActivityStack.remove(activity);
- 25. // mActivityStack.pop();
- 26. activity = null;
- 27. }
- 28. }
- 29.
- 30. // 获得当前栈顶Activity
- 31. public Activity currentActivity() {
- 32. Activity activity = mActivityStack.lastElement();
- 33. // Activity activity = mActivityStack.pop();
- 34. return activity;
- 35. }
- 36.
- 37. // 将当前Activity推入栈中
- 38. public void pushActivity(Activity activity) {
- 39. if (mActivityStack == null) {
- 40. mActivityStack = new Stack<Activity>();
- 41. }
- 42. mActivityStack.add(activity);
- 43. // mActivityStack.push(activity);
- 44. }
- 45.
- 46. // 退出栈中所有Activity
- 47. public void popAllActivityExceptOne(Class<Activity> cls) {
- 48. while (true) {
- 49. Activity activity = currentActivity();
- 50. if (activity == null) {
- 51. break;
- 52. }
- 53. if (activity.getClass().equals(cls)) {
- 54. break;
- 55. }
- 56. popActivity(activity);
- 57. }
- 58. }
- 59.
- 60.}
复制代码 logout.xml
Xml代码- 1.<?xml version="1.0" encoding="utf-8"?>
- 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 3. android:orientation="vertical"
- 4. android:layout_width="fill_parent"
- 5. android:layout_height="fill_parent"
- 6. >
- 7. <Button
- 8. android:id="@+id/btn1"
- 9. android:layout_width="fill_parent"
- 10. android:layout_height="wrap_content"
- 11. android:text="logout button1"
- 12. />
- 13. <Button
- 14. android:id="@+id/btn2"
- 15. android:layout_width="fill_parent"
- 16. android:layout_height="wrap_content"
- 17. android:text="logout button2"
- 18. />
- 19. <Button
- 20. android:id="@+id/btn3"
- 21. android:layout_width="fill_parent"
- 22. android:layout_height="wrap_content"
- 23. android:text="logout button3"
- 24. />
- 25. <Button
- 26. android:id="@+id/btn4"
- 27. android:layout_width="fill_parent"
- 28. android:layout_height="wrap_content"
- 29. android:text="go to first"
- 30. />
- 31. <Button
- 32. android:id="@+id/btn5"
- 33. android:layout_width="fill_parent"
- 34. android:layout_height="wrap_content"
- 35. android:text="go to launcher"
- 36. />
- 37. <Button
- 38. android:id="@+id/go"
- 39.
- 40. android:layout_width="fill_parent"
- 41. android:layout_height="wrap_content"
- 42. android:text="go another activity"
- 43. />
- 44. <!--
- 45. <EditText
- 46. android:id="@+id/et01"
- 47. android:layout_width="fill_parent"
- 48. android:layout_height="fill_parent"
- 49. />
- 50. <ImageView
- 51. android:id="@+id/iv01"
- 52. android:layout_width="wrap_content"
- 53. android:layout_height="wrap_content"
- 54. />
- 55. -->
- 56.</LinearLayout>
复制代码 manifest中的权限:
Xml代码- 1.<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
- 2.<uses-permission android:name="android.permission.RESTART_PACKAGE" />
复制代码 |