免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1974 | 回复: 1
打印 上一主题 下一主题

Android 应用程序多Activity跳转之后退出整个程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-13 22:47 |只看该作者 |倒序浏览
Android 应用程序多Activity跳转之后退出整个程序









在应用中肯定遇到有这样的问题,在应用中在于多的Activity中跳转,这些Activity都存在Activity栈中了。所以按返回键的时候都是一个一个的将原来的Activity弹回。如果我们想捕获到back事件之后直接退出整个程序,就要思考了。特别是2.2之后的安全机制考虑之后。

粘贴点代码,以备之后使用。



Java代码
  1. 1.package com.jftt;  
  2. 2.  
  3. 3.import android.app.Activity;  
  4. 4.import android.app.ActivityManager;  
  5. 5.import android.app.AlertDialog;  
  6. 6.import android.content.Context;  
  7. 7.import android.content.DialogInterface;  
  8. 8.import android.content.Intent;  
  9. 9.import android.os.Bundle;  
  10. 10.import android.util.Log;  
  11. 11.import android.view.KeyEvent;  
  12. 12.import android.view.View;  
  13. 13.import android.view.View.OnClickListener;  
  14. 14.import android.widget.Button;  
  15. 15.  
  16. 16.public class TestLogout extends Activity {  
  17. 17.    public static final String TAG = TestLogout.class.getSimpleName();  
  18. 18.    private Button btn1;  
  19. 19.    private Button btn2;  
  20. 20.    private Button btn3;  
  21. 21.    private Button btn4;  
  22. 22.    private Button btn5;  
  23. 23.    private Button go;  
  24. 24.  
  25. 25.    @Override  
  26. 26.    protected void onCreate(Bundle savedInstanceState) {  
  27. 27.        super.onCreate(savedInstanceState);  
  28. 28.        setContentView(R.layout.logout);  
  29. 29.        this.onStart();  
  30. 30.  
  31. 31.        btn1 = (Button) findViewById(R.id.btn1);  
  32. 32.        btn1.setOnClickListener(new OnClickListener() {  
  33. 33.            @Override  
  34. 34.            public void onClick(View v) {  
  35. 35.                android.os.Process.killProcess(android.os.Process.myPid()); // 获取PID  
  36. 36.            }  
  37. 37.        });  
  38. 38.  
  39. 39.        btn2 = (Button) findViewById(R.id.btn2);  
  40. 40.        btn2.setOnClickListener(new OnClickListener() {  
  41. 41.            @Override  
  42. 42.            public void onClick(View v) {  
  43. 43.                System.exit(0); // 常规java、c#的标准退出法,返回值为0代表正常退出  
  44. 44.            }  
  45. 45.        });  
  46. 46.  
  47. 47.        btn3 = (Button) findViewById(R.id.btn3);  
  48. 48.        btn3.setOnClickListener(new OnClickListener() {  
  49. 49.            @Override  
  50. 50.            public void onClick(View v) {  
  51. 51.                Log.i(TAG, "close " + getPackageName());  
  52. 52.                ActivityManager am = (ActivityManager) TestLogout.this .getSystemService(Context.ACTIVITY_SERVICE);  
  53. 53.                am.restartPackage(getPackageName());  
  54. 54.                // am.killBackgroundProcesses(getPackageName());  
  55. 55.            }  
  56. 56.        });  
  57. 57.         
  58. 58.        btn4 = (Button) findViewById(R.id.btn4);  
  59. 59.        btn4.setOnClickListener(new OnClickListener() {  
  60. 60.            @Override  
  61. 61.            public void onClick(View v) {  
  62. 62.                Intent intent = new Intent();  
  63. 63.                // intent.setClass((B或者C或者D).this, A.class);  
  64. 64.                intent.setClass(TestLogout.this, TestLogout.class);  
  65. 65.                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  66. 66.                intent.putExtra("flag", 1);  
  67. 67.                startActivity(intent);  
  68. 68.            }  
  69. 69.        });  
  70. 70.         
  71. 71.        //此方法并未杀掉应用程序 而是把launcher调起  
  72. 72.        btn5 = (Button) findViewById(R.id.btn5);  
  73. 73.        btn5.setOnClickListener(new OnClickListener() {  
  74. 74.            @Override  
  75. 75.            public void onClick(View v) {  
  76. 76.                Intent startMain = new Intent(Intent.ACTION_MAIN);  
  77. 77.                startMain.addCategory(Intent.CATEGORY_HOME);  
  78. 78.                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  79. 79.                startActivity(startMain);  
  80. 80.            }  
  81. 81.        });  
  82. 82.  
  83. 83.        go = (Button) findViewById(R.id.go);  
  84. 84.        go.setOnClickListener(new OnClickListener() {  
  85. 85.            @Override  
  86. 86.            public void onClick(View v) {  
  87. 87.                Intent intent = new Intent(TestLogout.this, TestLogout.class);  
  88. 88.                // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  89. 89.                startActivity(intent);  
  90. 90.            }  
  91. 91.        });  
  92. 92.  
  93. 93.    }  
  94. 94.      
  95. 95.    protected void onStart() {  
  96. 96.        super.onStart();  
  97. 97.        Intent intent = getIntent();  
  98. 98.        int x = intent.getIntExtra("flag", -1);  
  99. 99.        if (x == 0) {  
  100. 100.            finish();  
  101. 101.        }  
  102. 102.    }  
  103. 103.      
  104. 104.    @Override  
  105. 105.    public boolean onKeyDown(int keyCode, KeyEvent event) {  
  106. 106.        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  
  107. 107.            AlertDialog.Builder alertbBuilder = new AlertDialog.Builder(this);  
  108. 108.            alertbBuilder.setIcon(R.drawable.icon).setTitle("友情提示...").setMessage("你确定要离开吗?");  
  109. 109.            alertbBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  110. 110.                        @Override  
  111. 111.                        public void onClick(DialogInterface dialog, int which) {  
  112. 112.                            // 结束这个Activity  
  113. 113.                            int nPid = android.os.Process.myPid();  
  114. 114.                            android.os.Process.killProcess(nPid);  
  115. 115.                        }  
  116. 116.                    });  
  117. 117.            alertbBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
  118. 118.                        @Override  
  119. 119.                        public void onClick(DialogInterface dialog, int which) {  
  120. 120.                            dialog.cancel();  
  121. 121.                        }  
  122. 122.                    }).create();  
  123. 123.            alertbBuilder.show();  
  124. 124.        }  
  125. 125.        return true;  
  126. 126.    }  
  127. 127.}  
复制代码
Java代码
  1. 1.package com.jftt;  
  2. 2.  
  3. 3.import java.util.Stack;  
  4. 4.  
  5. 5.import android.app.Activity;  
  6. 6.  
  7. 7.public class ActiivtyStack {  
  8. 8.    private static Stack<Activity> mActivityStack;  
  9. 9.    private static ActiivtyStack instance;  
  10. 10.  
  11. 11.    private ActiivtyStack() {  
  12. 12.    }  
  13. 13.    public static ActiivtyStack getScreenManager() {  
  14. 14.        if (instance == null) {  
  15. 15.            instance = new ActiivtyStack();  
  16. 16.        }  
  17. 17.        return instance;  
  18. 18.    }  
  19. 19.  
  20. 20.    // 退出栈顶Activity  
  21. 21.    public void popActivity(Activity activity) {  
  22. 22.        if (activity != null) {  
  23. 23.            activity.finish();  
  24. 24.            mActivityStack.remove(activity);  
  25. 25.            // mActivityStack.pop();  
  26. 26.            activity = null;  
  27. 27.        }  
  28. 28.    }  
  29. 29.  
  30. 30.    // 获得当前栈顶Activity  
  31. 31.    public Activity currentActivity() {  
  32. 32.        Activity activity = mActivityStack.lastElement();  
  33. 33.        // Activity activity = mActivityStack.pop();  
  34. 34.        return activity;  
  35. 35.    }  
  36. 36.  
  37. 37.    // 将当前Activity推入栈中  
  38. 38.    public void pushActivity(Activity activity) {  
  39. 39.        if (mActivityStack == null) {  
  40. 40.            mActivityStack = new Stack<Activity>();  
  41. 41.        }  
  42. 42.        mActivityStack.add(activity);  
  43. 43.        // mActivityStack.push(activity);  
  44. 44.    }  
  45. 45.  
  46. 46.    // 退出栈中所有Activity  
  47. 47.    public void popAllActivityExceptOne(Class<Activity> cls) {  
  48. 48.        while (true) {  
  49. 49.            Activity activity = currentActivity();  
  50. 50.            if (activity == null) {  
  51. 51.                break;  
  52. 52.            }  
  53. 53.            if (activity.getClass().equals(cls)) {  
  54. 54.                break;  
  55. 55.            }  
  56. 56.            popActivity(activity);  
  57. 57.        }  
  58. 58.    }  
  59. 59.  
  60. 60.}  
复制代码
logout.xml



Xml代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>  
  2. 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.    android:orientation="vertical"  
  4. 4.    android:layout_width="fill_parent"  
  5. 5.    android:layout_height="fill_parent"  
  6. 6.    >  
  7. 7.    <Button  
  8. 8.        android:id="@+id/btn1"  
  9. 9.        android:layout_width="fill_parent"   
  10. 10.        android:layout_height="wrap_content"  
  11. 11.        android:text="logout button1"  
  12. 12.        />  
  13. 13.    <Button  
  14. 14.        android:id="@+id/btn2"  
  15. 15.        android:layout_width="fill_parent"   
  16. 16.        android:layout_height="wrap_content"  
  17. 17.        android:text="logout button2"  
  18. 18.        />  
  19. 19.    <Button  
  20. 20.        android:id="@+id/btn3"  
  21. 21.        android:layout_width="fill_parent"   
  22. 22.        android:layout_height="wrap_content"  
  23. 23.        android:text="logout button3"  
  24. 24.        />  
  25. 25.    <Button  
  26. 26.        android:id="@+id/btn4"  
  27. 27.        android:layout_width="fill_parent"   
  28. 28.        android:layout_height="wrap_content"  
  29. 29.        android:text="go to first"  
  30. 30.        />  
  31. 31.    <Button  
  32. 32.        android:id="@+id/btn5"  
  33. 33.        android:layout_width="fill_parent"   
  34. 34.        android:layout_height="wrap_content"  
  35. 35.        android:text="go to launcher"  
  36. 36.        />  
  37. 37.    <Button  
  38. 38.        android:id="@+id/go"  
  39. 39.  
  40. 40.        android:layout_width="fill_parent"   
  41. 41.        android:layout_height="wrap_content"  
  42. 42.        android:text="go another activity"  
  43. 43.        />  
  44. 44.    <!--  
  45. 45.    <EditText  
  46. 46.        android:id="@+id/et01"  
  47. 47.        android:layout_width="fill_parent"  
  48. 48.        android:layout_height="fill_parent"  
  49. 49.        />  
  50. 50.    <ImageView  
  51. 51.        android:id="@+id/iv01"  
  52. 52.        android:layout_width="wrap_content"  
  53. 53.        android:layout_height="wrap_content"  
  54. 54.        />  
  55. 55.    -->  
  56. 56.</LinearLayout>  
复制代码
manifest中的权限:



Xml代码
  1. 1.<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />  
  2. 2.<uses-permission android:name="android.permission.RESTART_PACKAGE" />
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-20 16:47 |只看该作者
学习啦   谢谢楼主
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP