Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 退出應用方法一:點擊兩次退出應用

android 退出應用方法一:點擊兩次退出應用

編輯:關於Android編程

(1)定義棧,寫一個Exitactivity 類,利用單例模式管理Activity,在每個在
Activity 的onCreate()方法中調用Exitactivity.getInstance().addActivity(this)方法,在退
出時調用Exitactivity.getInstance().exit()方法,就可以完全退出應用程序了。

(2)Exitactivity 類

public class Exitactivity extends Application {
	
	
	private List activityList=new LinkedList();

	 private static Exitactivity instance;
	
	 private Exitactivity()
	 {
	  }
	 //單例模式中獲取唯一的ExitApplication 實例
	 public static Exitactivity getInstance()
	 {
	  if(null == instance)
	   {
	     instance = new Exitactivity();
	   }
      return instance;
	
	 }
	 //添加Activity 到容器中
	 public void addActivity(Activity activity)
	 {
	 activityList.add(activity);
	 }
	 //遍歷所有Activity 並finish
	
	 public void exit()
	 {
	
	 for(Activity activity:activityList)
	  {
	    activity.finish();
	  }
	
	   System.exit(0);
	
	 }
	}

(3)其他的三個activity

1)activitymian的實現

package com.liuzuyi.exit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button next;
	private Button exit;
	 int i = 0;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		next=(Button)findViewById(R.id.button2);
		exit=(Button)findViewById(R.id.button1);
	
		next.setOnClickListener(l);
		
		exit.setOnClickListener(ll);
		 Exitactivity.getInstance().addActivity(this);
	  } 
	OnClickListener l = new OnClickListener() {
		
		public void onClick(View v) {
			 Intent intent=new Intent(MainActivity.this,TwoActivity.class);
			 startActivity(intent);		
		}
	};
	OnClickListener ll = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if( i == 0)
			{
			  Toast.makeText(MainActivity.this, "程序將在MainActivity退出,請再次點擊", Toast.LENGTH_LONG).show();
			  i =1;
			}
			else
			{
			  Exitactivity.getInstance().exit();	
			}
		}
	}; 
		
 
	}
	



2)TwoActivity的實現

package com.liuzuyi.exit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class TwoActivity extends Activity {

	private Button next;
	private Button exit;
	 int i = 0;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_two);
		next=(Button)findViewById(R.id.button2);
		exit=(Button)findViewById(R.id.button1);
	
		next.setOnClickListener(l);
		
		exit.setOnClickListener(ll);
		 Exitactivity.getInstance().addActivity(this);	
		
	}
OnClickListener l = new OnClickListener() {
		
		public void onClick(View v) {
			 Intent intent=new Intent(TwoActivity.this,ThreeActivity.class);
			 startActivity(intent);		
		}
	};
	OnClickListener ll = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if( i == 0 )
			 {
				Toast.makeText(TwoActivity.this, "程序將在TwoActivity退出,請再次點擊", Toast.LENGTH_LONG).show();
			    i =1;
			 }
			else
			Exitactivity.getInstance().exit();	
		}
	}; 
}

3)ThreeActivity的實現

package com.liuzuyi.exit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ThreeActivity extends Activity {

	private Button next;
	private Button exit;
	int i =0;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_three);
		next=(Button)findViewById(R.id.button2);
		exit=(Button)findViewById(R.id.button1);
		next.setOnClickListener(l);

		exit.setOnClickListener(ll);
		 Exitactivity.getInstance().addActivity(this);	
	}
   OnClickListener l = new OnClickListener() {
		
		public void onClick(View v) {
			 Intent intent=new Intent(ThreeActivity.this,MainActivity.class);
			 startActivity(intent);		
		}
	};
	OnClickListener ll = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			if( i   == 0)
			{
			     i =1;
				Toast.makeText(ThreeActivity.this, "程序將在ThreeActivity退出,請再次點擊", Toast.LENGTH_LONG).show();
			}
			else
			Exitactivity.getInstance().exit();		
		}
	}; 
}


可以在這裡下載源碼:

http://download.csdn.net/detail/liuzuyi200/7399541


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved