博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android退出程序
阅读量:5745 次
发布时间:2019-06-18

本文共 3458 字,大约阅读时间需要 11 分钟。

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(); } } }
View Code

BaseApplication

public class BaseApplication extends Application {        private static Stack
activityStack; 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) { } }}
View Code

AndroidMainifest.xml

....
View Code

MainAcitvity

public void exitApp{      BaseApplication.getInstance().AppExit();}
View Code

 

转载于:https://www.cnblogs.com/xmlovejaejoong/p/4778821.html

你可能感兴趣的文章
性能测试结果分析
查看>>
Linux-dns基础知识和BIND的简单配置-1
查看>>
kafka Corrupt index found
查看>>
PoE
查看>>
《JAVA与模式》之适配器模式
查看>>
采用spring+structs+hibanate框架开发javaWeb项目
查看>>
数据库和表的管理
查看>>
配置DNS服务器学习笔记
查看>>
搭建高性能内存对象缓存Memcached
查看>>
dns的正反向解析
查看>>
redis 常用命令
查看>>
LVS+Keepalived高可用负载均衡集群架构
查看>>
Linux 文本查看命令之sed
查看>>
Linux之内核编译基础概念
查看>>
11月TIOBE编程语言排行榜,OC已经掉出前十
查看>>
系统挂了,记录一下备份的东西
查看>>
Python多线程登录远端linux执行操作命令
查看>>
通往全栈工程师的捷径 —— react
查看>>
test命令
查看>>
C语言step-by-step(二)(数据类型)
查看>>