d7adcb50de86d59a114e4f7691202f93909eacd8.svn-base 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package eVVM.apk.common;
  2. import android.content.pm.ActivityInfo;
  3. import android.graphics.drawable.Drawable;
  4. import android.support.annotation.DrawableRes;
  5. import android.support.annotation.Nullable;
  6. import android.support.annotation.StringRes;
  7. import android.util.Log;
  8. import android.view.Gravity;
  9. import android.view.View;
  10. import com.hjq.bar.OnTitleBarListener;
  11. import com.hjq.bar.TitleBar;
  12. import com.hjq.base.BaseDialog;
  13. import com.hjq.dialog.MenuDialog;
  14. import com.hjq.toast.ToastUtils;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import butterknife.ButterKnife;
  18. import butterknife.Unbinder;
  19. import eVVM.apk.app.MyApplication;
  20. import eVVM.apk.helper.ActivityStackManager;
  21. import eVVM.apk.helper.DebugUtils;
  22. import eVVM.apk.helper.GPS.LocationUtils;
  23. import eVVM.apk.helper.NfcUtil;
  24. import eVVM.apk.other.EventBusManager;
  25. import eVVM.apk.other.StatusManager;
  26. public abstract class MyActivity extends UIActivity implements OnTitleBarListener {
  27. @Override
  28. protected void initActivity() {
  29. super.initActivity();
  30. ActivityStackManager.getInstance().onActivityCreated(this);
  31. }
  32. // ButterKnife 注解
  33. private Unbinder mButterKnife;
  34. @Override
  35. protected void initLayout() {
  36. super.initLayout();
  37. // 初始化标题栏的监听
  38. if (getTitleId() > 0) {
  39. if (findViewById(getTitleId()) instanceof TitleBar) {
  40. ((TitleBar) findViewById(getTitleId())).setOnTitleBarListener(this);
  41. }
  42. }
  43. mButterKnife = ButterKnife.bind(this);
  44. EventBusManager.register(this);
  45. initOrientation();
  46. }
  47. /**
  48. * 初始化横竖屏方向,会和 LauncherTheme 主题样式有冲突,注意不要同时使用
  49. */
  50. protected void initOrientation() {
  51. // 当前 Activity 不能是透明的并且没有指定屏幕方向,默认设置为竖屏
  52. if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
  53. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  54. }
  55. }
  56. /**
  57. * 设置标题栏的标题
  58. */
  59. @Override
  60. public void setTitle(int titleId) {
  61. setTitle(getText(titleId));
  62. }
  63. /**
  64. * 设置标题栏的标题
  65. */
  66. @Override
  67. public void setTitle(CharSequence title) {
  68. super.setTitle(title);
  69. TitleBar titleBar = getTitleBar();
  70. if (titleBar != null) {
  71. titleBar.setTitle(title);
  72. }
  73. }
  74. @Nullable
  75. public TitleBar getTitleBar() {
  76. if (getTitleId() > 0 && findViewById(getTitleId()) instanceof TitleBar) {
  77. return findViewById(getTitleId());
  78. }
  79. return null;
  80. }
  81. @Override
  82. public boolean statusBarDarkFont() {
  83. //返回true表示黑色字体
  84. return true;
  85. }
  86. /**
  87. * {@link OnTitleBarListener}
  88. */
  89. // TitleBar 左边的View被点击了
  90. @Override
  91. public void onLeftClick(View v) {
  92. onBackPressed();
  93. }
  94. // TitleBar 中间的View被点击了
  95. @Override
  96. public void onTitleClick(View v) {}
  97. // TitleBar 右边的View被点击了
  98. @Override
  99. public void onRightClick(View v) {}
  100. @Override
  101. protected void onResume() {
  102. super.onResume();
  103. // UmengClient.onResume(this);
  104. }
  105. @Override
  106. protected void onPause() {
  107. // UmengClient.onPause(this);
  108. super.onPause();
  109. }
  110. @Override
  111. protected void onDestroy() {
  112. super.onDestroy();
  113. if (mButterKnife != null) mButterKnife.unbind();
  114. EventBusManager.unregister(this);
  115. ActivityStackManager.getInstance().onActivityDestroyed(this);
  116. }
  117. private List<String> toastList = new ArrayList<>();
  118. /**
  119. * 显示吐司
  120. */
  121. public void toast(CharSequence s) {
  122. // ToastUtils.show(s);
  123. toastList.clear();
  124. toastList.add(s.toString());
  125. final BaseDialog show = new MenuDialog.Builder(this)
  126. .setCancel(null) // 设置 null 表示不显示取消按钮
  127. .setAutoDismiss(false) // 设置点击按钮后不关闭对话框
  128. .setList(toastList)
  129. .setGravity(Gravity.BOTTOM)
  130. .setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
  131. .show();
  132. postDelayed(new Runnable() {
  133. @Override
  134. public void run() {
  135. show.dismiss();
  136. }
  137. }, 2000);
  138. }
  139. public void toast(@StringRes int id) {
  140. ToastUtils.show(getString(id));
  141. }
  142. public void toast(Object object) {
  143. ToastUtils.show(object);
  144. }
  145. /**
  146. * 打印日志
  147. */
  148. public void log(Object object) {
  149. if (DebugUtils.isDebug(this)) {
  150. Log.v(getClass().getSimpleName(), object != null ? object.toString() : "null");
  151. }
  152. }
  153. /**
  154. * 获取当前的 Application 对象
  155. */
  156. public final MyApplication getMyApplication() {
  157. return (MyApplication) getApplication();
  158. }
  159. private final StatusManager mStatusManager = new StatusManager();
  160. /**
  161. * 显示加载中
  162. */
  163. public void showLoading() {
  164. mStatusManager.showLoading(this);
  165. }
  166. /**
  167. * 显示加载完成
  168. */
  169. public void showComplete() {
  170. mStatusManager.showComplete();
  171. }
  172. /**
  173. * 显示空提示
  174. */
  175. public void showEmpty() {
  176. mStatusManager.showEmpty(getContentView());
  177. }
  178. /**
  179. * 显示错误提示
  180. */
  181. public void showError() {
  182. mStatusManager.showError(getContentView());
  183. }
  184. /**
  185. * 显示自定义提示
  186. */
  187. public void showLayout(@DrawableRes int iconId, @StringRes int textId) {
  188. mStatusManager.showLayout(getContentView(), iconId, textId);
  189. }
  190. public void showLayout(Drawable drawable, CharSequence hint) {
  191. mStatusManager.showLayout(getContentView(), drawable, hint);
  192. }
  193. /**
  194. * 判断是否支持GPS和NFC功能
  195. */
  196. protected boolean checkGpsAndNfc(){
  197. switch (NfcUtil.isOPen(MyActivity.this)){
  198. case 0 :
  199. toast("您的手机没有NFC功能, 不能使用");
  200. return false;
  201. case 1 :
  202. toast("NFC功能未开启");
  203. return false;
  204. }
  205. if(!LocationUtils.isOPen(MyActivity.this)){
  206. toast("GPS功能未开启");
  207. return false;
  208. }
  209. return true;
  210. }
  211. }