GameService.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.jyc.threegames.service;
  2. import android.app.Notification;
  3. import android.app.Service;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.net.wifi.WifiManager;
  9. import android.os.Handler;
  10. import android.os.IBinder;
  11. import android.os.Message;
  12. import android.os.PowerManager;
  13. import android.widget.Toast;
  14. import androidx.annotation.NonNull;
  15. import androidx.annotation.Nullable;
  16. import com.jyc.threegames.App;
  17. import com.jyc.threegames.activity.AlertActivity;
  18. import com.jyc.threegames.activity.RingActivity;
  19. import com.jyc.threegames.bean.result.ResGameInfo;
  20. import com.jyc.threegames.controller.GameController;
  21. import com.jyc.threegames.controller.LoginController;
  22. import com.jyc.threegames.net.SimpleRequest;
  23. import com.jyc.threegames.utils.NotificationUtils;
  24. import com.jyc.threegames.utils.PhoneUtils;
  25. import com.orhanobut.logger.Logger;
  26. import java.util.Timer;
  27. import java.util.TimerTask;
  28. import io.reactivex.Observable;
  29. import io.reactivex.disposables.Disposable;
  30. public class GameService extends Service {
  31. private boolean mRequesting = false;
  32. private Handler mHandler = new Handler(new Handler.Callback() {
  33. @Override
  34. public boolean handleMessage(@NonNull Message message) {
  35. if (LoginController.getInstance().inTheTestPeriod() && System.currentTimeMillis() >= (App.LAST_TIME_CHECK_LOW_POWER + App.CHECK_TIME) && PhoneUtils.getBatteryLevel(App.app) <= 20){
  36. AlertActivity.LAUNCH(App.app, "你好,\n當前電量低於20%\n請及時充電!");
  37. App.LAST_TIME_CHECK_LOW_POWER = System.currentTimeMillis();
  38. }else if (LoginController.getInstance().inTheTestPeriod() && System.currentTimeMillis() >= (App.LAST_TIME_CHECK_NO_NET_WORK + App.CHECK_TIME) && !PhoneUtils.isNetworkConnected(App.app)){
  39. AlertActivity.LAUNCH(App.app, "你好,\n當前手機已斷網,\n請盡快連接網絡!");
  40. App.LAST_TIME_CHECK_NO_NET_WORK = System.currentTimeMillis();
  41. }else if (App.CAN_PLAY_GAME && LoginController.getInstance().getCurrentLoginInfo() != null && !LoginController.getInstance().isCurrentUserAdmin() && !mRequesting) {
  42. mRequesting = true;
  43. new SimpleRequest<ResGameInfo>().request(App.app, GameController.getInstance().needPlayGame(), new SimpleRequest.Executor<ResGameInfo>() {
  44. @Override
  45. public void execute(ResGameInfo obj) {
  46. if (App.CAN_PLAY_GAME && obj != null && obj.needDoGame) {
  47. Logger.i("need play game or fill scale");
  48. RingActivity.LAUNCH(App.app, obj);
  49. }
  50. mRequesting = false;
  51. }
  52. }, new SimpleRequest.Executor<Throwable>() {
  53. @Override
  54. public void execute(Throwable obj) {
  55. mRequesting = false;
  56. }
  57. });
  58. }
  59. return true;
  60. }
  61. });
  62. PowerManager.WakeLock mWakeLock;
  63. WifiManager.WifiLock mWifiLock;
  64. private Timer mTimer;
  65. @Nullable
  66. @Override
  67. public IBinder onBind(Intent intent) {
  68. return null;
  69. }
  70. @Override
  71. public void onCreate() {
  72. super.onCreate();
  73. startForeground(2, NotificationUtils.GET_NOTIFICATION(this, "Three Games", "Connected"));
  74. PowerManager mPowerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
  75. mWakeLock = mPowerManager.newWakeLock(
  76. PowerManager.PARTIAL_WAKE_LOCK, "Three_games:weak_lock");
  77. mWakeLock.acquire();
  78. WifiManager mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  79. mWifiLock = mWifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "three_games:wifi_lock");
  80. mWifiLock.acquire();
  81. mTimer = new Timer();
  82. mTimer.schedule(new TimerTask() {
  83. @Override
  84. public void run() {
  85. mHandler.sendEmptyMessage(0);
  86. }
  87. }, 0, 20 * 1000);
  88. }
  89. @Override
  90. public void onDestroy() {
  91. if (mTimer != null)
  92. mTimer.cancel();
  93. if (mWakeLock != null)
  94. mWakeLock.release();
  95. if (mWifiLock != null)
  96. mWifiLock.release();
  97. super.onDestroy();
  98. }
  99. }