編輯:關於Android編程
(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); } }
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
對於Android事件攔截機制,相信對於大多數Android初學者是一個抓耳撓腮難於理解的問題。其實理解這個問題並不困難。首先,你的明白事件攔截機制到底是怎麼一回事?這裡
推薦閱讀:淺析Android手機衛士自定義控件的屬性淺析Android手機衛士關閉自動更新詳解Android 手機衛士設置向導頁面一般的手機沒有root權限,進不去dat
本節學習系統中特殊的廣播接收者。我們前面幾節不是說了,當廣播接受者一旦注冊到系統中,當系統發送的廣播和你注冊的廣播的action匹配時,系統就會啟動廣播接收者所在的進程。
最終效果圖,點擊save會保存到文件中,點擊show會從文件中讀取出內容並顯示。main.xml<?xml version=1.0 encoding=utf