您的位置:首页 > 移动开发

Android中判断app何时是打开或者关闭的技术研究

2016-09-04 09:55 411 查看


存在的问题

Android开发中不可避免的会遇到需要检查app何时进入前台,何时被用户关闭。奇怪的是,要达到这个目的并不容易。检查app第一次启动并不难,但要判断它何时重新打开和关闭就没有那么简单了。这篇文章将介绍一种判断app打开,重新打开和关闭的技术。


让我们开始吧

判断一个app打开和关闭的关键在于判断它的activities是否正在前台显示。让我们先从简单的例子开始,一个只有一个activity的app,而且不支持水平模式。这样想要判断app是打开还是关闭只需要检查activity的onStart和onStop方法即可:

上面例子的问题在于当需要支持水平模式时该方法就失效了。当我们旋转设备时activity将会重建,onStart方法将被再次调用,这时将会错误的判断为app第二次被打开。为了处理设备旋转的情况,我们需要增加一个校验步骤。当activity退出时启动一个定时器,用于判断短时间内app的这个activity是否又被启动,如果没有,说明用户真的退出了这个app,如果重新启动了这个activity,说明用户还逗留在这个app中。这种校验方式也适用于拥有多个activities的app,因为从app的一个activity跳转到另一个activity也可以用这种校验方式来处理。使用这个技术我创建了一个管理类,所有的activities在可见和不可见时都会通知这个管理类。这个管理类为每个activity处理上述的校验步骤,从而避免错误的检测。它也提供了发布订阅(观察者)模式,任何对app启动和关闭感兴趣的模块都可以通过它来得到对应的通知。这个管理类的使用分为三个步骤:1)把它添加到你的工程中
/**
*Thisclassisresponsiblefortrackingallcurrentlyopenactivities.
*Bydoingsothisclasscandetectwhentheapplicationisintheforeground
*andwhenitisrunninginthebackground.
*/
publicclassAppForegroundStateManager{
privatestaticfinalStringTAG=AppForegroundStateManager.class.getSimpleName();
privatestaticfinalintMESSAGE_NOTIFY_LISTENERS=1;
publicstaticfinallongAPP_CLOSED_VALIDATION_TIME_IN_MS=
30*DateUtils.SECOND_IN_MILLIS;
//30Seconds
privateReference<Activity>mForegroundActivity;
privateSet<OnAppForegroundStateChangeListener>mListeners=
new
HashSet<>();
privateAppForegroundStatemAppForegroundState=
AppForegroundState.NOT_IN_FOREGROUND;
privateNotifyListenersHandlermHandler;
//Makethisclassathreadsafesingleton
privatestaticclassSingletonHolder{
publicstaticfinalAppForegroundStateManagerINSTANCE=
new
AppForegroundStateManager();
}
publicstaticAppForegroundStateManagergetInstance(){
return
SingletonHolder.INSTANCE;
}
privateAppForegroundStateManager(){
//Createthehandleronthemainthread
mHandler=
new
NotifyListenersHandler(Looper.getMainLooper());
}
publicenumAppForegroundState{
IN_FOREGROUND,
NOT_IN_FOREGROUND
}
publicinterfaceOnAppForegroundStateChangeListener{
/**Calledwhentheforegroundstateoftheappchanges*/
publicvoidonAppForegroundStateChange(AppForegroundStatenewState);
}
/**Anactivityshouldcallthiswhenitbecomesvisible*/
publicvoidonActivityVisible(Activityactivity){
if
(mForegroundActivity!=
null
)mForegroundActivity.clear();
mForegroundActivity=
new
WeakReference<>(activity);
determineAppForegroundState();
}
/**Anactivityshouldcallthiswhenitisnolongervisible*/
publicvoidonActivityNotVisible(Activityactivity){
/*
*Theforegroundactivitymayhavebeenreplacedwithanew
*
foregroundactivityinourapp.
*SoonlycleartheforegroundActivityifthenewactivitymatches
*
theforegroundactivity.
*/
if
(mForegroundActivity!=
null
){
Activityref=mForegroundActivity.get();
if
(activity==ref){
//Thisistheactivitythatisgoingaway,clearthereference
mForegroundActivity.clear();
mForegroundActivity=
null
;
}
}
determineAppForegroundState();
}
/**Usetodetermineifthisappisintheforeground*/
publicBooleanisAppInForeground(){
return
mAppForegroundState==AppForegroundState.IN_FOREGROUND;
}
/**
*Calltodeterminethecurrentstate,updatethetrackingglobal,and
*
notifysubscribersifthestatehaschanged.
*/
privatevoiddetermineAppForegroundState(){
/*Getthecurrentstate*/
AppForegroundStateoldState=mAppForegroundState;
/*Determinewhatthenewstateshouldbe*/
finalbooleanisInForeground=mForegroundActivity!=
null
&&mForegroundActivity.get()!=
null
;
mAppForegroundState=isInForeground?AppForegroundState.IN_FOREGROUND

:AppForegroundState.NOT_IN_FOREGROUND;
/*Ifthenewstateisdifferentthentheoldstatethenotifysubscribers
*
ofthestatechange
*/
if
(mAppForegroundState!=oldState){
validateThenNotifyListeners();
}
}
/**
*Addalistenertobenotifiedofappforegroundstatechangeevents.
*
*@paramlistener
*/
publicvoidaddListener(@NonNullOnAppForegroundStateChangeListenerlistener){
mListeners.add(listener);
}
/**
*Removealistenerfrombeingnotifiedofappforegroundstatechangeevents.
*
*@paramlistener
*/
publicvoidremoveListener(OnAppForegroundStateChangeListenerlistener){
mListeners.remove(listener);
}
/**Notifyalllistenerstheappforegroundstatehaschanged*/
privatevoidnotifyListeners(AppForegroundStatenewState){
android.util.Log.i(TAG,
"Notifyingsubscribersthatappjustenteredstate:"
+newState);
for
(OnAppForegroundStateChangeListenerlistener:mListeners){
listener.onAppForegroundStateChange(newState);
}
}
/**
*Thismethodwillnotifysubscribesthattheforegroundstatehaschangedwhen
*
andifappropriate.
*<br><br>
*Wedonotwanttojustnotifylistenersrightawaywhentheappentersof
*
leavestheforeground.Whenchangingorientationsoropeningand
*closingtheappquicklywebrieflypassthroughaNOT_IN_FOREGROUNDstatethat
*
mustbeignored.Toaccomplishthisadelayedmessagewillbe
*Sentwhenwedetectachange.Wewillnotnotifythataforegroundchange
*happeneduntilthedelaytimehasbeenreached.Ifasecond
*foregroundchangeisdetectedduringthedelayperiodthenthenotification
*
willbecanceled.
*/
privatevoidvalidateThenNotifyListeners(){
//Iftheapphasanypendingnotificationsthenthrowouttheeventasthe
//
statechangehasfailedvalidation
if
(mHandler.hasMessages(MESSAGE_NOTIFY_LISTENERS)){
android.util.Log.v(TAG,
"ValidationFailed:Throwingoutappforeground

statechangenotification"
);
mHandler.removeMessages(MESSAGE_NOTIFY_LISTENERS);
}
else
{
if
(mAppForegroundState==AppForegroundState.IN_FOREGROUND){
//Iftheappenteredtheforegroundthennotifylisteners
//
rightaway;thereisnovalidationtimeforthis
mHandler.sendEmptyMessage(MESSAGE_NOTIFY_LISTENERS);
}
else
{
//Weneedtovalidatethattheappenteredthebackground.Adelayis

//usedtoallowfortimewhentheapplicationwentintothe
//backgroundbutwedonotwanttoconsidertheappbeingbackgrounded

//suchasforinapppurchasingflowandfullscreenads.
mHandler.sendEmptyMessageDelayed(MESSAGE_NOTIFY_LISTENERS,
APP_CLOSED_VALIDATION_TIME_IN_MS);
}
}
}
privateclassNotifyListenersHandlerextendsHandler{
privateNotifyListenersHandler(Looperlooper){
super
(looper);
}
@Override
publicvoidhandleMessage(MessageinputMessage){
switch
(inputMessage.what){
//Thedecodingisdone
case
MESSAGE_NOTIFY_LISTENERS:
/*Notifysubscribersofthestatechange*/
android.util.Log.v(TAG,
"Appjustchangedforegroundstateto:"

+mAppForegroundState);
notifyListeners(mAppForegroundState);
break
;
default
:
super
.handleMessage(inputMessage);
}
}
}
}
2)Activities在可见性改变的需要发送通知app中所有activities都要增加下面的代码,用于可见性改变时通知管理类。最好的实现方式是把这段代码加到工程的BaseActivity中。

3)订阅app的前台可见性改变事件在感兴趣的模块中订阅app前台可见性改变事件,application类的onCreate函数是一个不错的地方,它可以保证每次app启动和关闭,你都能得到通知。


进一步的思考

有一些细节需要进一步讨论,下面讨论的几点针对具体的应用可以做微调。


校验时间

校验定时器检查app是否真的进入后台的时间间隔是多少合适呢?上面的代码设置为30秒,原因如下。当你的app在运行时,可能存在第三方的activities会覆盖全屏幕,一些常见的例子是Google应用内购买和Facebook登录注册页面。这些情况下你的app都会被迫进入后台,前台用于显示这些第三方页面。如果把这种情况当做用户离开了你的app,显然是不对的。30秒超时设置就是用来避免这种情况的。例如当用户在30秒内完成应用内购买,大部分用户都可以做得到,那么就不会当做用户突然离开app了。如果你的app不存在上述这种情况,我建议可以把你的校验时间设置为4秒,这样对于低配设备当屏幕旋转重新创建activity的时间间隔是合适的。


CPU休眠

可能存在的问题是当用户关闭app或者app仍处于前台时用户锁屏了,这时CPU可能不会等到定时器检测就休眠了。为了保证这种情况下定时器能够正常检测用户退出app,我们需要持有wakelock防止CPU休眠直到app关闭事件被确认。实践中相比使用wakelock,这种情况并不算问题。


判断app是如何启动的

现在我们已经知道如何检测app何时启动和关闭,但我们不知道app是如何启动的。是用户点击通知栏消息?还是点击一个链接?亦或是他们直接通过桌面图标或最近使用启动?


跟踪启动机制

首先我们需要知道在哪里检测app是如何启动的。基于前面一个例子我们可以打印出app何时启动,以及如何启动。


设置启动机制

现在我们可以打印app何时启动的机制,但我们没有设置它。因此下一步就是在用户通过链接或者通知启动app时我们记下它。如果没有通过这两种方式设置过,说明用户是通过点击app图标启动的。


跟踪链接点击事件

为了跟踪用户点击链接打开app,你需要找到代码中处理链接的地方,并加入下面的代码来跟踪启动机制。要确保这些代码在activity的onStart()函数之前调用。在哪些地方加入下面的代码取决于你的app架构了。getApplication().setLaunchMechanism(LaunchMechanism.URL);


跟踪通知事件

不幸的是跟踪通知点击需要更多技巧,通知显示后,点击它将会打开之前绑定好的一个PendingIntent,这里的技巧是为通知的所有PendingIntents添加一个标识表明是由通知发出的。例如当为通知创建PendingIntent时为每个intent添加如下代码:
publicstaticfinalStringEXTRA_HANDLING_NOTIFICATION=
"Notification.EXTRA_HANDLING_NOTIFICATION"
;
//Putanextrasoweknowwhenanactivitylaunchesif
//
itisafromanotification
intent.putExtra(EXTRA_HANDLING_NOTIFICATION,
true
);
到这一步我们需要做的就是在每个activity(统一在BaseActivity中添加)中检查这个标识。当识别到这个标识时说明是从通知启动的,这时可以把启动机制设置为通过通知。这一步应该在onCreate中处理,这样在app启动到前台之前就设置好了(会触发启动机制的打印)。本文接近尾声了,到这里你应该已经掌握了如何检测app何时启动和关闭,以及它是如何启动的。关于Java和Android大牛频道Java和Android大牛频道是一个数万人关注的探讨Java和Android开发的公众号,分享和原创最有价值的干货文章,让你成为这方面的大牛!我们探讨android和Java开发最前沿的技术:android性能优化,插件化,跨平台,动态化,加固和反破解等,也讨论设计模式/软件架构等。由一群来自BAT的工程师组成的团队。关注即送红包,回复:“百度”
、“阿里”、“腾讯”有惊喜!!!关注后可用入微信群。群里都是来自百度阿里腾讯的大牛。欢迎关注我们,一起讨论技术,扫描和长按下方的二维码可快速关注我们。或搜索微信公众号:JANiubility。公众号:JANiubility
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android