package com.jyc.threegames.service; 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.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 java.util.Timer; import java.util.TimerTask; public class GameService extends Service { // private BroadcastReceiver mReceiver = new BroadcastReceiver() { // @Override // public void onReceive(Context context, Intent intent) { // if (App.CAN_PLAY_GAME && LoginController.getInstance().getCurrentLoginInfo() != null && !LoginController.getInstance().isCurrentUserAdmin()) // new SimpleRequest().request(context, GameController.getInstance().needPlayGame(), new SimpleRequest.Executor() { // @Override // public void execute(ResGameInfo obj) { // if (App.CAN_PLAY_GAME && obj != null && obj.needDoGame){ // RingActivity.LAUNCH(App.app, obj); // } // } // }); // } // }; private Handler mHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message message) { if (App.CAN_PLAY_GAME && LoginController.getInstance().getCurrentLoginInfo() != null && !LoginController.getInstance().isCurrentUserAdmin()) new SimpleRequest().request(App.app, GameController.getInstance().needPlayGame(), new SimpleRequest.Executor() { @Override public void execute(ResGameInfo obj) { if (App.CAN_PLAY_GAME && obj != null && obj.needDoGame) { RingActivity.LAUNCH(App.app, obj); } } }); 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(); // IntentFilter intentFilter = new IntentFilter(); // intentFilter.addAction(Intent.ACTION_TIME_TICK); // registerReceiver(mReceiver, intentFilter); PowerManager pm = (PowerManager) this.getSystemService( Context.POWER_SERVICE); mWakeLock = pm.newWakeLock( PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "three_game:weak_lock"); mWakeLock.acquire(); WifiManager wMgr = (WifiManager) this.getSystemService(WIFI_SERVICE); mWifiLock = wMgr.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "three_game:wifi_lock"); mWifiLock.acquire(); mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { mHandler.sendEmptyMessage(0); } }, 0, 10 * 1000); } @Override public void onDestroy() { // unregisterReceiver(mReceiver); if (mTimer != null) mTimer.cancel(); if (mWakeLock != null) mWakeLock.release(); if (mWifiLock != null) mWifiLock.release(); super.onDestroy(); } }