71092da569b2ff16bc779f2d5e0936e3119fe36b.svn-base 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package com.example.myapplication.ui;
  2. import android.app.ActivityManager;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.text.TextUtils;
  7. import android.view.GestureDetector;
  8. import android.view.View;
  9. import android.widget.Toast;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. import com.example.myapplication.service.UserService;
  12. import com.example.myapplication.utils.BackGestureListener;
  13. import com.example.myapplication.utils.HttpUtils;
  14. import com.example.myapplication.utils.NoticeUtils;
  15. import com.example.myapplication.utils.SPUtils;
  16. import com.example.myapplication.R;
  17. import com.orhanobut.logger.Logger;
  18. import java.util.List;
  19. import io.reactivex.Observable;
  20. import io.reactivex.android.schedulers.AndroidSchedulers;
  21. import io.reactivex.functions.Consumer;
  22. import io.reactivex.functions.Function;
  23. import io.reactivex.schedulers.Schedulers;
  24. public abstract class BaseActivity extends AppCompatActivity {
  25. public static final long MAX_GESTURE_CHECK_TIME = 5 * 60 * 1000;
  26. public final String MOB_PAGE_NAME = this.getClass().getName();
  27. // 纯粹activity;如果包含fragment,设置false
  28. public boolean mMobIsNormalActivity = true;
  29. // public static final String MOB_EVENT_NAME = "click";
  30. /**
  31. * 手势监听
  32. */
  33. GestureDetector mGestureDetector;
  34. /**
  35. * 状态栏是否改变颜色,不需要改变的话,子类在super.oncreate()前调用
  36. */
  37. private boolean mShowStatusBarColor = true;
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. initGestureDetector();
  42. }
  43. /**
  44. * home键,返回时候,手势忘记密码
  45. */
  46. protected void checkLoginOut() {
  47. UserService.getInstance(this).logout();
  48. if (!(this instanceof MainActivity)) {
  49. startActivity(new Intent(this, MainActivity.class));
  50. }
  51. finish();
  52. }
  53. protected void hideCustomStatusBar() {
  54. mShowStatusBarColor = false;
  55. }
  56. @Override
  57. protected void onStop() {
  58. super.onStop();
  59. }
  60. /**
  61. * 从sp 获取缓存数据
  62. *
  63. * @param cacheFileName 缓存文件名
  64. * @param cacheKey 缓存key值
  65. * @param defaultValue 默认值
  66. * @param action 处理缓存值得action
  67. * @param <T> 返回缓存的值
  68. */
  69. public <T> void getCacheData(final String cacheFileName, final String cacheKey, final T defaultValue, Consumer<T> action) {
  70. Observable.just(defaultValue)
  71. .map(new Function<T, T>() {
  72. @Override
  73. public T apply(T t) {
  74. return (T) SPUtils.get(getApplicationContext(), cacheFileName, cacheKey, defaultValue);
  75. }
  76. })
  77. .subscribeOn(Schedulers.io())
  78. .observeOn(AndroidSchedulers.mainThread())
  79. .subscribe(action);
  80. }
  81. @Override
  82. protected void onStart() {
  83. super.onStart();
  84. }
  85. @Override
  86. protected void onPause() {
  87. super.onPause();
  88. }
  89. @Override
  90. protected void onResume() {
  91. super.onResume();
  92. }
  93. public void doBack(View view) {
  94. onBackPressed();
  95. }
  96. private void initGestureDetector() {
  97. if (mGestureDetector == null) {
  98. mGestureDetector =
  99. new GestureDetector(getApplicationContext(), new BackGestureListener(this));
  100. }
  101. }
  102. @Override
  103. protected void onDestroy() {
  104. super.onDestroy();
  105. }
  106. /**
  107. * 界面,右进左出 动画
  108. */
  109. public void startAnimActivity(Class<?> clazz) {
  110. startAnimActivity(clazz, null);
  111. }
  112. public void startAnimActivity(Class<?> clazz, Bundle bundle) {
  113. Intent intent = new Intent(this, clazz);
  114. if (bundle != null) {
  115. intent.putExtras(bundle);
  116. }
  117. startActivity(intent);
  118. overridePendingTransition(R.anim.right_in, R.anim.left_out);
  119. }
  120. public void startFadeActivity(Class<?> clazz) {
  121. startFadeActivity(clazz, null);
  122. }
  123. public void startFadeActivity(Class<?> clazz, Bundle bundle) {
  124. Intent intent = new Intent(this, clazz);
  125. if (bundle != null) {
  126. intent.putExtras(bundle);
  127. }
  128. startActivity(intent);
  129. overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
  130. }
  131. public boolean isNetworkAvailable() {
  132. return HttpUtils.isNetworkAvailable(this.getApplicationContext());
  133. }
  134. /**
  135. * findviewbyid
  136. */
  137. @SuppressWarnings("unchecked")
  138. public final <E extends View> E getView(int id) {
  139. try {
  140. return (E) findViewById(id);
  141. } catch (ClassCastException ex) {
  142. Logger.e(ex.getMessage());
  143. throw ex;
  144. }
  145. }
  146. public void finishAnim() {
  147. finish();
  148. overridePendingTransition(R.anim.left_in, R.anim.right_out);
  149. }
  150. @Override
  151. public void finish() {
  152. super.finish();
  153. }
  154. public void showToast(String message) {
  155. if (TextUtils.isEmpty(message)) {
  156. return;
  157. }
  158. NoticeUtils.showToast(this, message, Toast.LENGTH_SHORT);
  159. }
  160. /**
  161. * 程序是否在前台运行
  162. */
  163. public boolean isAppOnForeground() {
  164. ActivityManager activityManager = (ActivityManager) getApplicationContext()
  165. .getSystemService(Context.ACTIVITY_SERVICE);
  166. String packageName = getApplicationContext().getPackageName();
  167. /**
  168. * 获取Android设备中所有正在运行的App
  169. */
  170. List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
  171. .getRunningAppProcesses();
  172. if (appProcesses == null)
  173. return false;
  174. for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
  175. // The name of the process that this object is associated with.
  176. if (appProcess.processName.equals(packageName)
  177. && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. @Override
  184. public void onBackPressed() {
  185. super.onBackPressed();
  186. finishAnim();
  187. }
  188. }