0

i have a chat application.my code has a thread which manages internet connection. but after some minutes my thread goes sleep. I know i should use WakeLock to wake CPU and my thread. but when i use below code , battery usage goes up.

if (mWakeLock == null) {
                        mWakeLock = pm.newWakeLock(
                                PowerManager.PARTIAL_WAKE_LOCK,
                                "NewOnlineSignalingPowerManagerTag");
                    }
                    if (mWifiLock == null) {
                        mWifiLock = wm.createWifiLock(
                                WifiManager.WIFI_MODE_FULL,
                                "NewOnlineSignalingWifiManagerTag");
                    }
                    if (!mWakeLock.isHeld()) {
                        logging.l("mWakeLock.acquire()");
                        mWakeLock.acquire();
                    }
                    if (!mWifiLock.isHeld()) {
                        logging.l("mWifiLock.acquire()");
                        mWifiLock.acquire();
                    }

can you prefer me a better way?

M.Movaffagh
  • 1,284
  • 5
  • 20
  • 33

1 Answers1

0

Why do you have to manage a internet connection? You don't have to. Use a Android service, and there won't be a thread issue, nor a sleep issue.

nithinreddy
  • 6,167
  • 4
  • 38
  • 44
  • are you sure Service does not go to sleep? – M.Movaffagh Nov 27 '12 at 06:18
  • 1
    Yes, it won't. Ofcourse a Service can be terminated by Android if the memory is really low. But usually it doesn't. Also, you should be using Google's GCM (push service), if you are developing a chat application. So the app is automatically invoked, and there is no need of a service too. Its the optimal way for a chat app – nithinreddy Nov 27 '12 at 06:22