Commit b4372306 by zhengnw@sobot.com

widget 1.1.5 添加ActivityLifeCycle 检测app被系统回收(强杀)状态

parent 61bd877d
...@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) { ...@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) {
ext { ext {
PUBLISH_GROUP_ID = "com.sobot.library" //项目包名 PUBLISH_GROUP_ID = "com.sobot.library" //项目包名
PUBLISH_ARTIFACT_ID = 'widget_x' //项目名 PUBLISH_ARTIFACT_ID = 'widget_x' //项目名
PUBLISH_VERSION = '1.1.4' //版本号 PUBLISH_VERSION = '1.1.5' //版本号
} }
......
package com.sobot.widget.myappliction;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;
import com.sobot.utils.SobotLogUtils;
import java.lang.ref.WeakReference;
/**
* app 管理类
* 可判断 app被系统回收(强杀)状态;app是否前台
*/
public class SobotActivityLifeCycle implements Application.ActivityLifecycleCallbacks {
//默认被初始化状态,被系统回收(强杀)状态
public int mAppStatus = SobotAppStatusConstant.STATUS_FORCE_KILLED;
private boolean mPaused = true;
private boolean mForeground = false;
private static SobotActivityLifeCycle sInstance;
//当前Activity的弱引用
private WeakReference<Activity> mActivityReference;
protected final String TAG = "SobotActivityLifeCycle";
public static final int ACTIVITY_ON_RESUME = 0;
public static final int ACTIVITY_ON_PAUSE = 1;
//打开的Activity数量统计
private int activityStartCount = 0;
private SobotActivityLifeCycle() {
}
public static synchronized SobotActivityLifeCycle getInstance() {
if (sInstance == null) {
sInstance = new SobotActivityLifeCycle();
}
return sInstance;
}
/**
* 获取APP状态
*
* @return
*/
public int getAppStatus() {
return mAppStatus;
}
/**
* 设置APP状态
*
* @param appStatus
*/
public void setAppStatus(int appStatus) {
this.mAppStatus = appStatus;
}
public Activity getCurrentActivity() {
if (mActivityReference != null) {
return mActivityReference.get();
}
return null;
}
public boolean isForground() {
return mForeground;
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.d(TAG, activity.getLocalClassName() + " " + "onActivityCreated");
mActivityReference = new WeakReference<Activity>(activity);
}
@Override
public void onActivityStarted(Activity activity) {
Log.d(TAG, activity.getLocalClassName() + " " + "onActivityStarted");
activityStartCount++;
//数值从0变到1说明是从后台切到前台
if (activityStartCount == 1) {
//从后台切到前台
SobotLogUtils.i("从后台切到前台");
}
}
@Override
public void onActivityResumed(Activity activity) {
String activityName = activity.getClass().getName();
mPaused = false;
mForeground = true;
mActivityReference = new WeakReference<Activity>(activity);
}
@Override
public void onActivityPaused(Activity activity) {
//pause事件后是否在前台要分情况判断
mPaused = true;
}
@Override
public void onActivityStopped(Activity activity) {
Log.d(TAG, activity.getLocalClassName() + " " + "onActivityStopped");
activityStartCount--;
//数值从1到0说明是从前台切到后台
if (activityStartCount == 0) {
//从前台切到后台
SobotLogUtils.i("从前台切到后台");
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.d(TAG, activity.getLocalClassName() + " " + "onActivityDestroyed");
}
}
package com.sobot.widget.myappliction;
public class SobotAppStatusConstant {
public static final int STATUS_FORCE_KILLED = -1; //应用放在后台被强杀了
public static final int STATUS_NORMAL = 1; //APP正常态
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment