BaseActivity
public class BaseAcitivty extends Activity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); //全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //添加Activity到堆栈 BaseApplication.getInstance().addActivity(this); } @Override protected void onDestroy(){ super.onDestroy(); //结束Activity&从栈中移除该Activity BaseApplication.getInstance().finishActivity(this); } // HOME @Override public void onAttachedToWindow() { this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); } // 下拉 @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); try { Object service = getSystemService("statusbar"); Class statusbarManager = Class.forName("android.app.StatusBarManager"); Method test = statusbarManager.getMethod("collapse"); test.invoke(service); } catch (Exception ex) { ex.printStackTrace(); } } }
BaseApplication
public class BaseApplication extends Application { private static StackactivityStack; private static BaseApplication singleton; @Override public void onCreate() { super.onCreate(); singleton=this; } public static BaseApplication getInstance() { return singleton; } /** * add Activity 添加Activity到栈 */ public void addActivity(Activity activity){ if(activityStack ==null){ activityStack =new Stack (); } activityStack.add(activity); } /** * get current Activity 获取当前Activity(栈中最后一个压入的) */ public Activity currentActivity() { Activity activity = activityStack.lastElement(); return activity; } /** * 结束当前Activity(栈中最后一个压入的) */ public void finishActivity() { Activity activity = activityStack.lastElement(); finishActivity(activity); } /** * 结束指定的Activity */ public void finishActivity(Activity activity) { if (activity != null) { activityStack.remove(activity); activity.finish(); activity = null; } } /** * 结束指定类名的Activity */ public void finishActivity(Class cls) { for (Activity activity : activityStack) { if (activity.getClass().equals(cls)) { finishActivity(activity); } } } /** * 结束所有Activity */ public void finishAllActivity() { for (int i = 0, size = activityStack.size(); i < size; i++) { if (null != activityStack.get(i)) { activityStack.get(i).finish(); } } activityStack.clear(); } /** * 退出应用程序 */ public void AppExit() { try { finishAllActivity(); } catch (Exception e) { } }}
AndroidMainifest.xml
....
MainAcitvity
public void exitApp{ BaseApplication.getInstance().AppExit();}