大家给点评论好不好,我发上来是希望看看大家的意见想法,赞对我没太大的意义
为什么我要封装 application
application 在以前是非常重要的一个地方,刚开始开发时我们会在 application 中存储数据,提供全局公共方法,application 虽然很方便,但是渐渐的我们还是放弃了在 application 里面做其他的工作,现在 application 主要的应用场景是:
但是我在使用 application 时还是遇到了一些问题不好处理:
- 平台化时,公司一个团队要同时开发多个 app,不同的 app 有不同的 application 实现类,这样我们在业务module 里怎么统一方便的拿到 application
- application 没有提供 启动,退出,切到后台,前台的相应方法
- 没有优雅的退出 app 的方法
- 没有能拿到 app 当前状态的地方,ActivityManage 又存在适配问题,有的设备 TM 的就是出幺蛾子
我想大家都曾为了上面的问题烦恼过吧,也许你已经解决了,也许你只是临时解决了,也许你还没解决。同样这也困扰过我,以前我只是临时解决,代码耦合难看,还没发适应越来越高的复用要求,一处该,处处动,这样的代码不是我想要的,早就该扫到垃圾箱里去了,于是诞生了今天的文章
我对 application 是这样安排的:
- 使用代理统一方便的对外提供 application 上下文对象
- 自己计数,准确的提供 app 启动,退出,切到后台,前台的回调
- 自己维护 app 当前的状态
- 仿照 Lifecycle 的思路,把相应的生命周期回调改成响应式
- 优雅的退出 app
话说官方的 Lifecycle 刚出来时我是非常兴奋的,我想这下 app 的生命周期该好用了吧,兴奋的我一看官方压根就没动 application,只是把 Activity 的声明周期函数响应式化了,我是很失望的,也许 google 考虑兼容性吧
回顾下 ActivityLifecycleCallbacks
application 的核心就是这个了,一切的基础都是他在身上做的
public class Application extends ContextWrapper implements ComponentCallbacks2 {
private ArrayList<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks = new ArrayList<ActivityLifecycleCallbacks>();
public interface ActivityLifecycleCallbacks {
void onActivityCreated(Activity activity, Bundle savedInstanceState);
void onActivityStarted(Activity activity);
void onActivityResumed(Activity activity);
void onActivityPaused(Activity activity);
void onActivityStopped(Activity activity);
void onActivitySaveInstanceState(Activity activity, Bundle outState);
void onActivityDestroyed(Activity activity);
}
}
复制代码
我们平时使用 ActivityLifecycleCallbacks 基本都是这个样子的
this.registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
Log.d("AA", "onActivityCreated")
}
override fun onActivityResumed(activity: Activity?) {
Log.d("AA", "onActivityResumed")
}
override fun onActivityStarted(activity: Activity?) {
Log.d("AA", "onActivityStarted")
}
override fun onActivityPaused(activity: Activity?) {
Log.d("AA", "AA")
}
override fun onActivityDestroyed(activity: Activity?) {
Log.d("AA", "onActivityDestroyed")
}
override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
Log.d("AA", "onActivitySaveInstanceState")
}
override fun onActivityStopped(activity: Activity?) {
Log.d("AA", "onActivityStopped")
}
})
复制代码
跟进源码看看
public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
synchronized (mActivityLifecycleCallbacks) {
mActivityLifecycleCallbacks.add(callback);
}
}
复制代码
注意看是 add 方法,这说明啥,这说明 ActivityLifecycleCallbacks 我们可以添加多个,Activity 会在相应的生命周期函数中发射相关消息
protected void onCreate(@Nullable Bundle savedInstanceState) {
getApplication().dispatchActivityCreated(this, savedInstanceState);
}
void dispatchActivityCreated(Activity activity, Bundle savedInstanceState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityCreated(activity,
savedInstanceState);
}
}
}
复制代码
我们添加 2个 ActivityLifecycleCallbacks 进去
事实证明,的确可以添加多个回调,并且没有问题,这也是我们自己封装 application 组件的基石
类结构
我就不画 UML 类图,因为很简单
考虑到我要在 application 组件里添加很多功能进来,那么良好合理的分层
就必不可少了,即便是该功能非常简单,直接在 ApplicationManage 里实现更方便,但是基于单一职责原则
必须分工明确,现在费点劲,以后省大事
我设计了以下几个角色:
- ApplicationManage - 提供统一入口,对外提供功能,内部封装管理各细分功能实现和相互交互
- exit - 用于优雅退出 app
- ExitActivity - 拦截 app 退出事件
- ExitManage - 管理 app 退出功能,对 ApplicationManage 提供功能实现
- ExitMessage - app 退出相关参数
- Lifecycle - app 生命周期改响应式
- LifecycleManage - 用于发射相关消息
- LifecycleMessage - 消息数据类型
- StateManage - app 状态管理
Lifecycle 和 StateManage 都是私有的,不是应该外部可见的。外部只需要注册生命周期的 observer,不需要知道我怎么实现的。同样 外部不需要知道我们怎么管理的 app 当前状态,只需要知道现在 app 是个什么样子。这部分我通过 ApplicationManage 对外提供相应方法
另外我在写消息类型和 app 状态类型时,我考虑了下相应的 type 放在哪里合适,是相应 manage 的内部类,还是专门一个类。内部类方便外界使用,专门的类方便查看代码结构,这就得看实际场景了,Lifecycle 这块我是用的内部类做的,这样逻辑顺延好些代码,exit 这块基本都是对内的,不暴露出来,所以专门维护了一个类
使用效果
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
ScreenAutoManager.instance.init(this, 1080.0f, ScreenAutoManager.BASE_LINE_WIDTH)
ApplicationManage.init(this)
ApplicationManage.instance.addObserver { lifecycleMessage ->
when (lifecycleMessage.type) {
ApplicationManage.MessageType.MESSAGE_ACTIVITY_CREATE -> {
if (lifecycleMessage.activity != null) ScreenAutoManager.instance.onActivityCreated(lifecycleMessage.activity)
Log.d("AA", "MESSAGE_ACTIVITY_CREATE")
}
ApplicationManage.MessageType.MESSAGE_ACTIVITY_START -> {
if (lifecycleMessage.activity != null) ScreenAutoManager.instance.onActivityStarted(lifecycleMessage.activity)
Log.d("AA", "MESSAGE_ACTIVITY_START")
}
ApplicationManage.MessageType.MESSAGE_ACTIVITY_RESUME -> {
if (lifecycleMessage.activity != null) ScreenAutoManager.instance.onActivityResumed(lifecycleMessage.activity)
Log.d("AA", "MESSAGE_ACTIVITY_RESUME")
}
ApplicationManage.MessageType.MESSAGE_ACTIVITY_PAUSE -> Log.d("AA", "MESSAGE_ACTIVITY_PAUSE")
ApplicationManage.MessageType.MESSAGE_ACTIVITY_STOP -> Log.d("AA", "MESSAGE_ACTIVITY_STOP")
ApplicationManage.MessageType.MESSAGE_ACTIVITY_SAVEINSTANCESTATE -> Log.d("AA", "MESSAGE_ACTIVITY_SAVEINSTANCESTATE")
ApplicationManage.MessageType.MESSAGE_ACTIVITY_DESTROYED -> Log.d("AA", "MESSAGE_ACTIVITY_DESTROYED")
ApplicationManage.MessageType.MESSAGE_APP_START -> Log.d("AA", "MESSAGE_APP_START")
ApplicationManage.MessageType.MESSAGE_APP_EXIT -> Log.d("AA", "MESSAGE_APP_EXIT")
ApplicationManage.MessageType.MESSAGE_APP_FORNT -> Log.d("AA", "MESSAGE_APP_FORNT")
ApplicationManage.MessageType.MESSAGE_APP_BACKGROUD -> Log.d("AA", "MESSAGE_APP_BACKGROUD")
}
}
}
复制代码
改造 application 生命周期管理
我个人是很喜欢 Lifecycle 的,官方人家可是用的 apt 做的,我注解这块写不好,借助的是 Livedata,在 registerActivityLifecycleCallbacks 时发射相应的消息出来
class LifecycleManage {
var lifecycleLivaData: MyLiveData<LifecycleMessage> = MyLiveData()
/**
* 初始化方法
*/
fun init(application: Application) {
registerActivityLifecycleCallbacks(application)
}
/**
* 注册 application 生命周期回调函数,在对应的函数回调中发射对应的消息
*/
private fun registerActivityLifecycleCallbacks(application: Application) {
if (application == null) return
application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_CREATE, activity = activity, savedInstanceState = savedInstanceState))
}
override fun onActivityStarted(activity: Activity?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_START, activity = activity))
}
override fun onActivityResumed(activity: Activity?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_RESUME, activity = activity))
}
override fun onActivityPaused(activity: Activity?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_PAUSE, activity = activity))
}
override fun onActivityStopped(activity: Activity?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_STOP, activity = activity))
}
override fun onActivityDestroyed(activity: Activity?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_DESTROYED, activity = activity))
}
override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_SAVEINSTANCESTATE, activity = activity, savedInstanceState = outState))
}
})
}
}
复制代码
然后大家通过 ApplicationManage 实现注册 observer
fun addObserver(tag: String? = null, lifecycle: Lifecycle? = null, observer: (lifecycleMessage: LifecycleMessage) -> Unit) {
lifecycleManage.lifecycleLivaData.addObserver(tag, lifecycle, observer)
}
复制代码
然后是使用
ApplicationManage.instance.addObserver { lifecycleMessage ->
when (lifecycleMessage.type) {
ApplicationManage.MessageType.MESSAGE_ACTIVITY_CREATE -> {
if (lifecycleMessage.activity != null) ScreenAutoManager.instance.onActivityCreated(lifecycleMessage.activity)
Log.d("AA", "MESSAGE_ACTIVITY_CREATE")
}
ApplicationManage.MessageType.MESSAGE_ACTIVITY_START -> {
if (lifecycleMessage.activity != null) ScreenAutoManager.instance.onActivityStarted(lifecycleMessage.activity)
Log.d("AA", "MESSAGE_ACTIVITY_START")
}
ApplicationManage.MessageType.MESSAGE_ACTIVITY_RESUME -> {
if (lifecycleMessage.activity != null) ScreenAutoManager.instance.onActivityResumed(lifecycleMessage.activity)
Log.d("AA", "MESSAGE_ACTIVITY_RESUME")
}
ApplicationManage.MessageType.MESSAGE_ACTIVITY_PAUSE -> Log.d("AA", "MESSAGE_ACTIVITY_PAUSE")
ApplicationManage.MessageType.MESSAGE_ACTIVITY_STOP -> Log.d("AA", "MESSAGE_ACTIVITY_STOP")
ApplicationManage.MessageType.MESSAGE_ACTIVITY_SAVEINSTANCESTATE -> Log.d("AA", "MESSAGE_ACTIVITY_SAVEINSTANCESTATE")
ApplicationManage.MessageType.MESSAGE_ACTIVITY_DESTROYED -> Log.d("AA", "MESSAGE_ACTIVITY_DESTROYED")
ApplicationManage.MessageType.MESSAGE_APP_START -> Log.d("AA", "MESSAGE_APP_START")
ApplicationManage.MessageType.MESSAGE_APP_EXIT -> Log.d("AA", "MESSAGE_APP_EXIT")
ApplicationManage.MessageType.MESSAGE_APP_FORNT -> Log.d("AA", "MESSAGE_APP_FORNT")
ApplicationManage.MessageType.MESSAGE_APP_BACKGROUD -> Log.d("AA", "MESSAGE_APP_BACKGROUD")
}
}
复制代码
可以看到这里 app 的启动,退出,前后台切换我也都发送相关的消息了
实现不难,思路就使用 livedata 在合适的位置转发一下数据,官方的 Lifecycle 也是这个思路做的
判断 app 启动,退出,前后台切换
我想大家都关心这个,相信大家也都有自己的实现,基本思路都是在 registerActivityLifecycleCallbacks 里更新计数器判断 app 状态,这里我也是一样的,只不过多了一步罢了
我标识了 app 的3种状态: app state 默认是 no - app没启动
val STATE_APP_NO: Int = 31
val STATE_APP_FORNT: Int = 32
val STATE_APP_BACKGROUD: Int = 33
复制代码
判断逻辑如下:
- app 启动 - 触发 create 函数,state = STATE_APP_NO,说明 app 之前没启动,那么此时必然就是启动了
- app 退出 - 触发 destroy 函数,若活跃 activity 计数 =0 了,那说明 app 就是关闭了
- app 切入后台 - 触发 stop 函数,活跃 activity 计数先--,若 =0 了并且 state = STATE_APP_FORNT,那就是说明前台没有活跃显示的 activity 了,进入后台了
- app 切回前台 - 触发 start 函数,先判断 活跃 activity 计数,若 =0 了并且 state = STATE_APP_BACKGROUD,那说明又切回前台来了,计数器再++
注意:onActivityStarted 这个函数即表示 onStart,也表示 onRestart ,所以在计数时要更外小心,因为 start 的原因 oncreate 不适合计数器++了,要不会和 start 重复
这样我们也可以返回 app 当前的状态了,不用再用 AM 来判断了,AM 很多人反应部分手机无效,其实返回 app 状态的这个小功能非常实用的,在推送时我们要判断 app 是不是启来了,因为由不同的操作
优雅的退出 app
我这里思路很简单,就是主界面下面垫一个透明无界面的 Activity,我们想退出 app 时启动这个 Activity,在 onNewIntent 里面 finish 页面就行了,不过这个功能有侵入行:
- 一是需要屏蔽主界面的 onBackPressed 函数,不控制返回键的话就回到这个透明的页面了
- 二是闪屏页启动主界面时需要使用该功能的启动页面方法
// 退出 app
ApplicationManage.instance.exitmanage.exitApp()
// 代理方法启动主界面
ApplicationManage.instance.exitmanage.startActivityProxy(this, Intent(this@SplaseActivity, MainActivity::class.java))
// 屏蔽主界面返回按键默认操作
override fun onBackPressed() {
// super.onBackPressed()
}
复制代码
具体代码
我知道大家都懒得看 demo,这代码长也得贴出来
/**
* 作者 : BloodCrown
* 时间 : 2019-05-08 21:33
* 描述 :
* application 封装类,在方便提供 application 上下文对象的同时,
* 也提供了一些功能
*
* 功能 :
* 1. Application 上下文代理,任何模块都不必关心 application 具体的实现类型
* 有 ApplicationManage 在时刻都能获取全局上下文对象
*
* 2. 提供优雅退出 app 的功能,通过在主页面下面添加一个透明的 activity + singleTask 实现优雅退出
*
* 3. Application 的生命周期实现响应式,像 EventBus,RxBus 那样响应消息就行,另外我还添加了
* app 启动,退出,切入后台,切回前台的相应进来
*
* 4. 在实现功能3时,实现了 app 当前状态的保存,极大的方便了我们在注入推送时判断 app 是否启动等操作
*
* 最后说一点,我没有使用 ActivityManage 来判断 app 状态,因为 ActivityManage 存在适配问题,
* 总有那么一小撮手机就是不配合,臣妾也是没办法呀~
*
*/
class ApplicationManage {
companion object Help {
// 饿汉式单例,加上同步限制,这样可以避免 application 操作类单例为null 的情况
val instance: ApplicationManage by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
return@lazy ApplicationManage()
}
// 标准初始化方法,
fun init(application: Application) {
instance.init(application)
}
}
// 全局上下文对象
var application: Application? = null
// 退出 app 工具类
var exitmanage: ExitManage = ExitManage()
// app 全局生命周期管理类,使用 livedata 实现,考虑其若是外部可见,就能使用他乱发射数据,数据私有
private var lifecycleManage: LifecycleManage = LifecycleManage()
// app 状态管理类,也是私有,这个实现绝对是不对外的,大家关心结果就好了,由 ApplicationManage 衔接
private var stateManage: StateManage = StateManage()
/**
* 初始化方法私有,由静态方法衔接
*/
private fun init(application: Application) {
this.application = application
lifecycleManage.init(application)
stateManage.initStateManage(lifecycleManage)
}
/**
* 添加监听
*/
fun addObserver(tag: String? = null, lifecycle: Lifecycle? = null, observer: (lifecycleMessage: LifecycleMessage) -> Unit) {
lifecycleManage.lifecycleLivaData.addObserver(tag, lifecycle, observer)
}
/**
* 解绑
*/
fun removeobserver(tag: String) {
lifecycleManage.lifecycleLivaData.removeOberver(tag)
}
/**
* 获取当前 app 状态
*/
fun getCurrentState(): Int {
return stateManage.STAET_CURRENT
}
/**
* 消息类型
*/
class MessageType {
companion object {
// 对应 activity 的生命周期
@JvmField
val MESSAGE_ACTIVITY_CREATE: Int = 11
@JvmField
val MESSAGE_ACTIVITY_RESUME: Int = 12
@JvmField
val MESSAGE_ACTIVITY_START: Int = 13
@JvmField
val MESSAGE_ACTIVITY_PAUSE: Int = 14
@JvmField
val MESSAGE_ACTIVITY_STOP: Int = 15
@JvmField
val MESSAGE_ACTIVITY_DESTROYED: Int = 16
@JvmField
val MESSAGE_ACTIVITY_SAVEINSTANCESTATE: Int = 17
// app 启动,退出,切换到前台,切换到后台
@JvmField
val MESSAGE_APP_START: Int = 21
@JvmField
val MESSAGE_APP_EXIT: Int = 25
@JvmField
val MESSAGE_APP_FORNT: Int = 22
@JvmField
val MESSAGE_APP_BACKGROUD: Int = 23
}
}
}
复制代码
/**
* 作者 : BloodCrown
* 时间 : 2019-05-08 21:38
* 描述 :
*/
class LifecycleManage {
var lifecycleLivaData: MyLiveData<LifecycleMessage> = MyLiveData()
/**
* 初始化方法
*/
fun init(application: Application) {
registerActivityLifecycleCallbacks(application)
}
/**
* 注册 application 生命周期回调函数,在对应的函数回调中发射对应的消息
*/
private fun registerActivityLifecycleCallbacks(application: Application) {
if (application == null) return
application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_CREATE, activity = activity, savedInstanceState = savedInstanceState))
}
override fun onActivityStarted(activity: Activity?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_START, activity = activity))
}
override fun onActivityResumed(activity: Activity?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_RESUME, activity = activity))
}
override fun onActivityPaused(activity: Activity?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_PAUSE, activity = activity))
}
override fun onActivityStopped(activity: Activity?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_STOP, activity = activity))
}
override fun onActivityDestroyed(activity: Activity?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_DESTROYED, activity = activity))
}
override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
lifecycleLivaData.sendValue(LifecycleMessage(type = ApplicationManage.MessageType.MESSAGE_ACTIVITY_SAVEINSTANCESTATE, activity = activity, savedInstanceState = outState))
}
})
}
}
复制代码
class LifecycleMessage(var type: Int, var activity: Activity? = null, var savedInstanceState: Bundle? = null) {
}
复制代码
/**
* 作者 : BloodCrown
* 时间 : 2019-05-09 21:32
* 描述 :
*/
class StateManage {
companion object stateType {
// 没启动,前台,后台
@JvmField
val STATE_APP_NO: Int = 31
@JvmField
val STATE_APP_FORNT: Int = 32
@JvmField
val STATE_APP_BACKGROUD: Int = 33
}
var STAET_CURRENT: Int = STATE_APP_NO
var aliveActivitys: Int = 0
fun initStateManage(lifecycleManage: LifecycleManage) {
if (lifecycleManage == null) return
addObserver(lifecycleManage)
}
/**
* 添加管理器
*/
private fun addObserver(lifecycleManage: LifecycleManage) {
if (lifecycleManage == null) return
lifecycleManage.lifecycleLivaData.addObserver {
when (it.type) {
ApplicationManage.MessageType.MESSAGE_ACTIVITY_CREATE -> {
// 标记是没启动,那么触发 create 一定是app 启动
if (STAET_CURRENT == STATE_APP_NO) {
STAET_CURRENT = STATE_APP_FORNT
lifecycleManage.lifecycleLivaData.sendValue(LifecycleMessage(ApplicationManage.MessageType.MESSAGE_APP_START))
}
}
ApplicationManage.MessageType.MESSAGE_ACTIVITY_START -> {
// 活动 ac 数量为0,并且当前标记是 app 在后台,那么此时触发 start,那么就是切会到前台来了
if (aliveActivitys == 0 && STAET_CURRENT == STATE_APP_BACKGROUD) {
STAET_CURRENT = STATE_APP_FORNT
lifecycleManage.lifecycleLivaData.sendValue(LifecycleMessage(ApplicationManage.MessageType.MESSAGE_APP_FORNT))
}
aliveActivitys++
}
ApplicationManage.MessageType.MESSAGE_ACTIVITY_STOP -> {
aliveActivitys--
if (aliveActivitys == 0 && STAET_CURRENT == STATE_APP_FORNT) {
STAET_CURRENT = STATE_APP_BACKGROUD
lifecycleManage.lifecycleLivaData.sendValue(LifecycleMessage(ApplicationManage.MessageType.MESSAGE_APP_BACKGROUD))
}
}
ApplicationManage.MessageType.MESSAGE_ACTIVITY_DESTROYED -> {
if (aliveActivitys == 0) {
STAET_CURRENT = STATE_APP_NO
lifecycleManage.lifecycleLivaData.sendValue(LifecycleMessage(ApplicationManage.MessageType.MESSAGE_APP_EXIT))
}
}
}
}
}
}
复制代码
class ExitActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_exit)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent == null) return
val action: Int = intent.getIntExtra(ExitMessage.MESSAGE_ACTION, ExitMessage.ACTION_EXIT)
when (action) {
ExitMessage.ACTION_EXIT -> this@ExitActivity.finish()
else -> this@ExitActivity.finish()
}
}
}
复制代码
class ExitManage {
fun exitApp(): Boolean {
if (ApplicationManage.instance.application == null) return false
var intent = Intent(ApplicationManage.instance.application, ExitActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
ApplicationManage.instance.application?.startActivity(intent)
return true
}
fun exitAppBySystem() {
System.exit(0)
}
fun startActivityProxy(activity: Activity, intentYou: Intent) {
if (activity == null) return
var intentExit = Intent(activity, ExitActivity::class.java)
intentExit.putExtra(ExitMessage.MESSAGE_ACTION, ExitMessage.ACTION_EXIT)
activity.startActivity(intentExit)
activity.startActivity(intentYou)
}
}
复制代码
object ExitMessage {
val MESSAGE_ACTION: String = "message_action"
val ACTION_EXIT: Int = 101
}
复制代码
能看到这里的都是相当给面子的了~
最后吐槽下吧
锁屏,解锁时我即便按照下面的设置设了
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize|keyboardHidden"
复制代码
但是蛋疼的有的手机就是不给面子,还是会走1-2遍 activity 销毁,重建的过程,太蛋疼了,手头的 meizu 16h就这样,好在这样的手机只是少部分,但是带给我们的影响就是前后台切换的消息会走好几次,这我也没办法,昨天下午一下午的时间就是在找资料搞订这个,一下午的时间过去了也不行,算了就这样吧,大家记得android 这啃爹玩意完事不是 100% 有些偏差就得了,我监听锁屏,解锁广播,发现最后才收到广播,声明周期函数早就执行完了,广播才来还有个P用啊
唉,一下午的时间身心俱疲啊~
我知道高玩们是看不上我这篇文章的,不喷我就是好的了,全当是自娱自乐吧