123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package com.jyc.threegames.service;
- import android.app.Notification;
- import android.app.Service;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.net.wifi.WifiManager;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Message;
- import android.os.PowerManager;
- import android.widget.Toast;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import com.jyc.threegames.App;
- import com.jyc.threegames.activity.AlertActivity;
- import com.jyc.threegames.activity.RingActivity;
- import com.jyc.threegames.bean.result.ResGameInfo;
- import com.jyc.threegames.controller.GameController;
- import com.jyc.threegames.controller.LoginController;
- import com.jyc.threegames.net.SimpleRequest;
- import com.jyc.threegames.utils.NotificationUtils;
- import com.jyc.threegames.utils.PhoneUtils;
- import com.orhanobut.logger.Logger;
- import java.util.Timer;
- import java.util.TimerTask;
- import io.reactivex.Observable;
- import io.reactivex.disposables.Disposable;
- public class GameService extends Service {
- private boolean mRequesting = false;
- private Handler mHandler = new Handler(new Handler.Callback() {
- @Override
- public boolean handleMessage(@NonNull Message message) {
- if (LoginController.getInstance().inTheTestPeriod() && System.currentTimeMillis() >= (App.LAST_TIME_CHECK_LOW_POWER + App.CHECK_TIME) && PhoneUtils.getBatteryLevel(App.app) <= 20){
- AlertActivity.LAUNCH(App.app, "你好,\n當前電量低於20%\n請及時充電!");
- App.LAST_TIME_CHECK_LOW_POWER = System.currentTimeMillis();
- }else if (LoginController.getInstance().inTheTestPeriod() && System.currentTimeMillis() >= (App.LAST_TIME_CHECK_NO_NET_WORK + App.CHECK_TIME) && !PhoneUtils.isNetworkConnected(App.app)){
- AlertActivity.LAUNCH(App.app, "你好,\n當前手機已斷網,\n請盡快連接網絡!");
- App.LAST_TIME_CHECK_NO_NET_WORK = System.currentTimeMillis();
- }else if (App.CAN_PLAY_GAME && LoginController.getInstance().getCurrentLoginInfo() != null && !LoginController.getInstance().isCurrentUserAdmin() && !mRequesting) {
- mRequesting = true;
- new SimpleRequest<ResGameInfo>().request(App.app, GameController.getInstance().needPlayGame(), new SimpleRequest.Executor<ResGameInfo>() {
- @Override
- public void execute(ResGameInfo obj) {
- if (App.CAN_PLAY_GAME && obj != null && obj.needDoGame) {
- Logger.i("need play game or fill scale");
- RingActivity.LAUNCH(App.app, obj);
- }
- mRequesting = false;
- }
- }, new SimpleRequest.Executor<Throwable>() {
- @Override
- public void execute(Throwable obj) {
- mRequesting = false;
- }
- });
- }
- return true;
- }
- });
- PowerManager.WakeLock mWakeLock;
- WifiManager.WifiLock mWifiLock;
- private Timer mTimer;
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- startForeground(2, NotificationUtils.GET_NOTIFICATION(this, "Three Games", "Connected"));
- PowerManager mPowerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
- mWakeLock = mPowerManager.newWakeLock(
- PowerManager.PARTIAL_WAKE_LOCK, "Three_games:weak_lock");
- mWakeLock.acquire();
- WifiManager mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
- mWifiLock = mWifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "three_games:wifi_lock");
- mWifiLock.acquire();
- mTimer = new Timer();
- mTimer.schedule(new TimerTask() {
- @Override
- public void run() {
- mHandler.sendEmptyMessage(0);
- }
- }, 0, 20 * 1000);
- }
- @Override
- public void onDestroy() {
- if (mTimer != null)
- mTimer.cancel();
- if (mWakeLock != null)
- mWakeLock.release();
- if (mWifiLock != null)
- mWifiLock.release();
- super.onDestroy();
- }
- }
|