2016-09-16 62 views
0

我有应用程序与计时器。当我按下电源按钮时,我的应用进入睡眠模式并停止计时。我应该怎么做计时器继续工作。下面我非常简单的代码计时器。Android应用不能在睡眠模式下工作

Thread t = new Thread(){ 
    @Override 
    public void run() { 
     while (!isInterrupted()){ 
      try { 
       Thread.sleep(1000); 
       i++; 
       Log.e("pokaz "," i "+i); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
}; 
+2

看看这个:http://stackoverflow.com/questions/10725711/how-to-do-not-let-thread-stop-theme-self-when-in-standby-mode – 476rick

回答

0

我解决了我的问题。我写在活动

PowerManager pm; 
    PowerManager.WakeLock wl; 
    pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE); 
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); 
    wl.acquire(); 

和布局这个代码编写代码

android:keepScreenOn="true" 

但现在我的应用程序在后台和版本API是奇巧evrything是确定的。当在API lolipop中测试我的应用程序并进入睡眠模式,然后打开手机时,我看到消息“不幸myApp已停止”。为什么?

-1

若要您的应用程序在后台,你需要使用ServiceIntentServiceBroadCastReceiver

而你的线程工作Service

相关问题