Android 关闭所有的Activity

/** * 使用方法:* 1、所有的Activity继承这个baseActivity** 2、要退出时,调用this.clossAllActivity()**/
 

 
  
package com.abc.util;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * function:关闭所有的Activity
 * 
 * 基类的Activity,每个Activity都继承自这个基类Activity,
 * 在这个基类Activity中写个广播,当程序退出的时候,发送关闭Activity的广播。
 *
 */
public class BaseActivity extends Activity{
	
	public static final String ACTION_CLOSS_ACTIVITY = "com.abc.BaseActivity.finish";
	
	private BroadcastReceiver broadcastReceiver = new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			// 结束窗口
			BaseActivity.this.finish();
		}
	};

	@Override
	//--activity真正在前台运行中
	protected void onResume() {
		super.onResume();
		// 在当前的activity中注册广播 
		IntentFilter filter = new IntentFilter();
		filter.addAction(ACTION_CLOSS_ACTIVITY);
		this.registerReceiver(broadcastReceiver, filter);
	}
	

	//Activity has leaked IntentReceiver  that was originally registered here. 
	//Are you missing a call to unregisterReceiver()?
	//注册了广播接收器后要在销毁时取消注册,否则会出错。
	@Override
	protected void onDestroy() {
		this.unregisterReceiver(broadcastReceiver);
		super.onDestroy();
	}

	public void closeAllActivity() {
		// 发送广播,关闭所有activity
		Intent intent = new Intent();
		intent.setAction(ACTION_CLOSS_ACTIVITY);
		sendBroadcast(intent);//--发送当前的意图
	}
}


 

 
  
 
 

你可能感兴趣的:(Android)