在应用中肯定遇到有这样的问题,在应用中在于多的Activity中跳转,这些Activity都存在Activity栈中了。所以按返回键的时候都是一个一个的将原来的Activity弹回。如果我们想捕获到back事件之后直接退出整个程序,就要思考了。特别是2.2之后的安全机制考虑之后。
粘贴点代码,以备之后使用。
- packagecom.jftt;
- importandroid.app.Activity;
- importandroid.app.ActivityManager;
- importandroid.app.AlertDialog;
- importandroid.content.Context;
- importandroid.content.DialogInterface;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.KeyEvent;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- publicclassTestLogoutextendsActivity{
- publicstaticfinalStringTAG=TestLogout.class.getSimpleName();
- privateButtonbtn1;
- privateButtonbtn2;
- privateButtonbtn3;
- privateButtonbtn4;
- privateButtonbtn5;
- privateButtongo;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.logout);
- this.onStart();
- btn1=(Button)findViewById(R.id.btn1);
- btn1.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- android.os.Process.killProcess(android.os.Process.myPid());//获取PID
- }
- });
- btn2=(Button)findViewById(R.id.btn2);
- btn2.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- System.exit(0);//常规java、c#的标准退出法,返回值为0代表正常退出
- }
- });
- btn3=(Button)findViewById(R.id.btn3);
- btn3.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- Log.i(TAG,"close"+getPackageName());
- ActivityManageram=(ActivityManager)TestLogout.this.getSystemService(Context.ACTIVITY_SERVICE);
- am.restartPackage(getPackageName());
- //am.killBackgroundProcesses(getPackageName());
- }
- });
- btn4=(Button)findViewById(R.id.btn4);
- btn4.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- Intentintent=newIntent();
- //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(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- IntentstartMain=newIntent(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(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- Intentintent=newIntent(TestLogout.this,TestLogout.class);
- //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- }
- });
- }
- protectedvoidonStart(){
- super.onStart();
- Intentintent=getIntent();
- intx=intent.getIntExtra("flag",-1);
- if(x==0){
- finish();
- }
- }
- @Override
- publicbooleanonKeyDown(intkeyCode,KeyEventevent){
- if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
- AlertDialog.BuilderalertbBuilder=newAlertDialog.Builder(this);
- alertbBuilder.setIcon(R.drawable.icon).setTitle("友情提示...").setMessage("你确定要离开吗?");
- alertbBuilder.setPositiveButton("确定",newDialogInterface.OnClickListener(){
- @Override
- publicvoidonClick(DialogInterfacedialog,intwhich){
- //结束这个Activity
- intnPid=android.os.Process.myPid();
- android.os.Process.killProcess(nPid);
- }
- });
- alertbBuilder.setNegativeButton("取消",newDialogInterface.OnClickListener(){
- @Override
- publicvoidonClick(DialogInterfacedialog,intwhich){
- dialog.cancel();
- }
- }).create();
- alertbBuilder.show();
- }
- returntrue;
- }
- }
- packagecom.jftt;
- importjava.util.Stack;
- importandroid.app.Activity;
- publicclassActiivtyStack{
- privatestaticStack<Activity>mActivityStack;
- privatestaticActiivtyStackinstance;
- privateActiivtyStack(){
- }
- publicstaticActiivtyStackgetScreenManager(){
- if(instance==null){
- instance=newActiivtyStack();
- }
- returninstance;
- }
- //退出栈顶Activity
- publicvoidpopActivity(Activityactivity){
- if(activity!=null){
- activity.finish();
- mActivityStack.remove(activity);
- //mActivityStack.pop();
- activity=null;
- }
- }
- //获得当前栈顶Activity
- publicActivitycurrentActivity(){
- Activityactivity=mActivityStack.lastElement();
- //Activityactivity=mActivityStack.pop();
- returnactivity;
- }
- //将当前Activity推入栈中
- publicvoidpushActivity(Activityactivity){
- if(mActivityStack==null){
- mActivityStack=newStack<Activity>();
- }
- mActivityStack.add(activity);
- //mActivityStack.push(activity);
- }
- //退出栈中所有Activity
- publicvoidpopAllActivityExceptOne(Class<Activity>cls){
- while(true){
- Activityactivity=currentActivity();
- if(activity==null){
- break;
- }
- if(activity.getClass().equals(cls)){
- break;
- }
- popActivity(activity);
- }
- }
- }
logout.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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="logoutbutton1"
- />
- <Button
- android:id="@+id/btn2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="logoutbutton2"
- />
- <Button
- android:id="@+id/btn3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="logoutbutton3"
- />
- <Button
- android:id="@+id/btn4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="gotofirst"
- />
- <Button
- android:id="@+id/btn5"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="gotolauncher"
- />
- <Button
- android:id="@+id/go"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="goanotheractivity"
- />
- <!--
- <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中的权限:
- <uses-permissionandroid:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
- <uses-permissionandroid:name="android.permission.RESTART_PACKAGE"/>
退出Android应用的多种方式
本文介绍了在Android应用中实现退出功能的多种方法,包括直接杀死进程、重启包、调用Launcher等,并提供了一个Activity栈管理类来帮助管理多个Activity。



1358

被折叠的 条评论
为什么被折叠?



