不解释了,直接说下测试的例子
例子:三个Activity之间任意切换,然后在任意一个界面按下模拟器上的返回键,观察是否退出应用;
先创建一个广播接收器:
package com.study.test; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyBroadCast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //Activity被销毁时如果没有解除注册,系统会报异常 ((Activity)context).unregisterReceiver(this); ((Activity)context).finish(); } }
工具类:
package com.study.test; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; public class Util { Activity activity; public Util(Activity activity){ this.activity = activity; } /** * 注册广播 */ public void registerBroadcastReceiver() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("CLOSE"); activity.registerReceiver(new MyBroadCast(), intentFilter); } /** * 关闭Activity */ public void exitApp(){ Intent intent =new Intent(); intent.setAction("CLOSE"); activity.sendBroadcast(intent); activity.finish(); } }
MainActivity
package com.study.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button btn1; Button btn2; TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.tv); tv.setText("===========MainActivity==========="); btn1 = (Button) findViewById(R.id.btn1); btn1.setText("to AActivity"); btn1.setOnClickListener(new ButtonClickListener()); btn2 = (Button) findViewById(R.id.btn2); btn2.setText("to BActivity"); btn2.setOnClickListener(new ButtonClickListener()); } public class ButtonClickListener implements View.OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn1: startActivity(new Intent(MainActivity.this, AActivity.class)); break; case R.id.btn2 : startActivity(new Intent(MainActivity.this, BActivity.class)); break; } } } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); //注册广播 new Util(this).registerBroadcastReceiver(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode == KeyEvent.KEYCODE_BACK) { System.out.println("---------------->main"); new Util(this).exitApp(); } return super.onKeyDown(keyCode, event); } }
AActivity
package com.study.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; public class AActivity extends Activity { Button btn1; Button btn2; TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.tv); tv.setText("AActivity------------->"); btn1 = (Button) findViewById(R.id.btn1); btn1.setText("to MainActivity"); btn1.setOnClickListener(new ButtonClickListener()); btn2 = (Button) findViewById(R.id.btn2); btn2.setText("to BActivity"); btn2.setOnClickListener(new ButtonClickListener()); } public class ButtonClickListener implements View.OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn1: startActivity(new Intent(AActivity.this, MainActivity.class)); break; case R.id.btn2 : startActivity(new Intent(AActivity.this, BActivity.class)); break; } } } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); new Util(this).registerBroadcastReceiver(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode == KeyEvent.KEYCODE_BACK) { System.out.println("---------------->a"); new Util(this).exitApp(); } return super.onKeyDown(keyCode, event); } }
BActivity
package com.study.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; public class BActivity extends Activity { Button btn1; Button btn2; TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.tv); tv.setText("BActivity------------->"); btn1 = (Button) findViewById(R.id.btn1); btn1.setText("to MainActivity"); btn1.setOnClickListener(new ButtonClickListener()); btn2 = (Button) findViewById(R.id.btn2); btn2.setText("to AActivity"); btn2.setOnClickListener(new ButtonClickListener()); } public class ButtonClickListener implements View.OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn1: startActivity(new Intent(BActivity.this, MainActivity.class)); break; case R.id.btn2 : startActivity(new Intent(BActivity.this, AActivity.class)); break; } } } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); //注册广播 new Util(this).registerBroadcastReceiver(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode == KeyEvent.KEYCODE_BACK) { System.out.println("---------------->BActivity界面"); new Util(this).exitApp(); } return super.onKeyDown(keyCode, event); } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tv" android:textSize="20dp" android:textColor="@android:color/white" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/btn1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/btn2" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="18dp" android:text="测试开始,三个Activity之间任意切换,然后在任何界面按下返回键都会安全的关闭整个应用" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.study.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".AActivity"/> <activity android:name=".BActivity"/> </application> </manifest>
测试界面:
源码下载链接:
http://download.csdn.net/detail/sdefzhpk/4329553