package com.example.myapplication.ui; import android.app.ActivityManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.GestureDetector; import android.view.View; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.myapplication.service.UserService; import com.example.myapplication.utils.BackGestureListener; import com.example.myapplication.utils.HttpUtils; import com.example.myapplication.utils.NoticeUtils; import com.example.myapplication.utils.SPUtils; import com.example.myapplication.R; import com.orhanobut.logger.Logger; import java.util.List; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.functions.Consumer; import io.reactivex.functions.Function; import io.reactivex.schedulers.Schedulers; public abstract class BaseActivity extends AppCompatActivity { public static final long MAX_GESTURE_CHECK_TIME = 5 * 60 * 1000; public final String MOB_PAGE_NAME = this.getClass().getName(); // 纯粹activity;如果包含fragment,设置false public boolean mMobIsNormalActivity = true; // public static final String MOB_EVENT_NAME = "click"; /** * 手势监听 */ GestureDetector mGestureDetector; /** * 状态栏是否改变颜色,不需要改变的话,子类在super.oncreate()前调用 */ private boolean mShowStatusBarColor = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initGestureDetector(); } /** * home键,返回时候,手势忘记密码 */ protected void checkLoginOut() { UserService.getInstance(this).logout(); if (!(this instanceof MainActivity)) { startActivity(new Intent(this, MainActivity.class)); } finish(); } protected void hideCustomStatusBar() { mShowStatusBarColor = false; } @Override protected void onStop() { super.onStop(); } /** * 从sp 获取缓存数据 * * @param cacheFileName 缓存文件名 * @param cacheKey 缓存key值 * @param defaultValue 默认值 * @param action 处理缓存值得action * @param 返回缓存的值 */ public void getCacheData(final String cacheFileName, final String cacheKey, final T defaultValue, Consumer action) { Observable.just(defaultValue) .map(new Function() { @Override public T apply(T t) { return (T) SPUtils.get(getApplicationContext(), cacheFileName, cacheKey, defaultValue); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(action); } @Override protected void onStart() { super.onStart(); } @Override protected void onPause() { super.onPause(); } @Override protected void onResume() { super.onResume(); } public void doBack(View view) { onBackPressed(); } private void initGestureDetector() { if (mGestureDetector == null) { mGestureDetector = new GestureDetector(getApplicationContext(), new BackGestureListener(this)); } } @Override protected void onDestroy() { super.onDestroy(); } /** * 界面,右进左出 动画 */ public void startAnimActivity(Class clazz) { startAnimActivity(clazz, null); } public void startAnimActivity(Class clazz, Bundle bundle) { Intent intent = new Intent(this, clazz); if (bundle != null) { intent.putExtras(bundle); } startActivity(intent); overridePendingTransition(R.anim.right_in, R.anim.left_out); } public void startFadeActivity(Class clazz) { startFadeActivity(clazz, null); } public void startFadeActivity(Class clazz, Bundle bundle) { Intent intent = new Intent(this, clazz); if (bundle != null) { intent.putExtras(bundle); } startActivity(intent); overridePendingTransition(R.anim.fade_in, R.anim.fade_out); } public boolean isNetworkAvailable() { return HttpUtils.isNetworkAvailable(this.getApplicationContext()); } /** * findviewbyid */ @SuppressWarnings("unchecked") public final E getView(int id) { try { return (E) findViewById(id); } catch (ClassCastException ex) { Logger.e(ex.getMessage()); throw ex; } } public void finishAnim() { finish(); overridePendingTransition(R.anim.left_in, R.anim.right_out); } @Override public void finish() { super.finish(); } public void showToast(String message) { if (TextUtils.isEmpty(message)) { return; } NoticeUtils.showToast(this, message, Toast.LENGTH_SHORT); } /** * 程序是否在前台运行 */ public boolean isAppOnForeground() { ActivityManager activityManager = (ActivityManager) getApplicationContext() .getSystemService(Context.ACTIVITY_SERVICE); String packageName = getApplicationContext().getPackageName(); /** * 获取Android设备中所有正在运行的App */ List appProcesses = activityManager .getRunningAppProcesses(); if (appProcesses == null) return false; for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { // The name of the process that this object is associated with. if (appProcess.processName.equals(packageName) && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false; } @Override public void onBackPressed() { super.onBackPressed(); finishAnim(); } }