GameService.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.jyc.threegames.service;
  2. import android.app.Service;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.net.wifi.WifiManager;
  8. import android.os.Handler;
  9. import android.os.IBinder;
  10. import android.os.Message;
  11. import android.os.PowerManager;
  12. import android.widget.Toast;
  13. import androidx.annotation.NonNull;
  14. import androidx.annotation.Nullable;
  15. import com.jyc.threegames.App;
  16. import com.jyc.threegames.activity.RingActivity;
  17. import com.jyc.threegames.bean.result.ResGameInfo;
  18. import com.jyc.threegames.controller.GameController;
  19. import com.jyc.threegames.controller.LoginController;
  20. import com.jyc.threegames.net.SimpleRequest;
  21. import java.util.Timer;
  22. import java.util.TimerTask;
  23. public class GameService extends Service {
  24. // private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  25. // @Override
  26. // public void onReceive(Context context, Intent intent) {
  27. // if (App.CAN_PLAY_GAME && LoginController.getInstance().getCurrentLoginInfo() != null && !LoginController.getInstance().isCurrentUserAdmin())
  28. // new SimpleRequest<ResGameInfo>().request(context, GameController.getInstance().needPlayGame(), new SimpleRequest.Executor<ResGameInfo>() {
  29. // @Override
  30. // public void execute(ResGameInfo obj) {
  31. // if (App.CAN_PLAY_GAME && obj != null && obj.needDoGame){
  32. // RingActivity.LAUNCH(App.app, obj);
  33. // }
  34. // }
  35. // });
  36. // }
  37. // };
  38. private Handler mHandler = new Handler(new Handler.Callback() {
  39. @Override
  40. public boolean handleMessage(@NonNull Message message) {
  41. if (App.CAN_PLAY_GAME && LoginController.getInstance().getCurrentLoginInfo() != null && !LoginController.getInstance().isCurrentUserAdmin())
  42. new SimpleRequest<ResGameInfo>().request(App.app, GameController.getInstance().needPlayGame(), new SimpleRequest.Executor<ResGameInfo>() {
  43. @Override
  44. public void execute(ResGameInfo obj) {
  45. if (App.CAN_PLAY_GAME && obj != null && obj.needDoGame) {
  46. RingActivity.LAUNCH(App.app, obj);
  47. }
  48. }
  49. });
  50. return true;
  51. }
  52. });
  53. PowerManager.WakeLock mWakeLock;
  54. WifiManager.WifiLock mWifiLock;
  55. private Timer mTimer;
  56. @Nullable
  57. @Override
  58. public IBinder onBind(Intent intent) {
  59. return null;
  60. }
  61. @Override
  62. public void onCreate() {
  63. super.onCreate();
  64. // IntentFilter intentFilter = new IntentFilter();
  65. // intentFilter.addAction(Intent.ACTION_TIME_TICK);
  66. // registerReceiver(mReceiver, intentFilter);
  67. PowerManager pm = (PowerManager) this.getSystemService(
  68. Context.POWER_SERVICE);
  69. mWakeLock = pm.newWakeLock(
  70. PowerManager.PARTIAL_WAKE_LOCK
  71. | PowerManager.ON_AFTER_RELEASE,
  72. "three_game:weak_lock");
  73. mWakeLock.acquire();
  74. WifiManager wMgr = (WifiManager) this.getSystemService(WIFI_SERVICE);
  75. mWifiLock = wMgr.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "three_game:wifi_lock");
  76. mWifiLock.acquire();
  77. mTimer = new Timer();
  78. mTimer.schedule(new TimerTask() {
  79. @Override
  80. public void run() {
  81. mHandler.sendEmptyMessage(0);
  82. }
  83. }, 0, 10 * 1000);
  84. }
  85. @Override
  86. public void onDestroy() {
  87. // unregisterReceiver(mReceiver);
  88. if (mTimer != null)
  89. mTimer.cancel();
  90. if (mWakeLock != null)
  91. mWakeLock.release();
  92. if (mWifiLock != null)
  93. mWifiLock.release();
  94. super.onDestroy();
  95. }
  96. }