我夕
不解释了,直接说下测试的例子
例子:三个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);
}
}
布局文件:
AndroidManifest.xml
测试界面:
源码下载链接:
http://download.csdn.net/detail/sdefzhpk/4329553