4

I am using a countdown timer in a service.The timer goes on like that 01,02,03,.... Everything works fine.But when the device screen lock the timer becomes slow but still running.I am using the PowerManager but not working.PowerManager only works when the USB cable connected to the PC but when i remove the device from PC timer slows down.I do not know why this problem arises.Please help me to sort out this problem.Thank in advace.Below is my code.

 @Override
    public void onCreate(){
        super.onCreate();

        PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "whatever");
        wl.acquire();

        }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        flagVibrate=intent.getBooleanExtra("FlagVibrate",false);
        if(mStartTime == 0L){
            mStartTime = SystemClock.uptimeMillis();
            mHandler.removeCallbacks(mUpdateTimeTask);
            mHandler.postDelayed(mUpdateTimeTask, 100);

        }
    }

        @Override
    public void onDestroy() {

        super.onDestroy();
        mHandler.removeCallbacks(mUpdateTimeTask);

        stopSelf();
        wl.release();
    }
        private Runnable mUpdateTimeTask = new Runnable(){

        public void run() {

        final long start = mStartTime;
        long millis = SystemClock.uptimeMillis()- start;

        int seconds = (int) (millis / 1000);
        int stopTime = (int) (millis / 1000);
        int minutes = seconds / 60;
        seconds = seconds % 60;

        GuardMeActivity.timerView.setText("" + minutes + ":"
                              + String.format("%02d", seconds));

        timerStop1 = minutes + ":"
                  + String.format("%02d", seconds);



        }

}

};
Deepak Sharma
  • 4,999
  • 5
  • 51
  • 61

1 Answers1

2

Write down following code in your GuardMeActivity activity's onResume() method and check the result.

Files to import

import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
Vigbyor
  • 2,568
  • 4
  • 21
  • 35