2012-05-15 72 views
0

我在随机播放提醒用户的服务。该服务由一个Activity启动并作为服务在后台运行。电话睡着时播放声音

我面临的挑战是,当用户将手机置于睡眠状态(空白屏幕)时,在某些情况下,声音根本无法播放。时间已用完,但声音不会按时播放,或在用户唤醒电话时播放。

下面是一些代码 /** * 开始被检查每秒钟多少时间 *过去了,如果时间点已经到了播放声音一个新的线程。 *一旦时间到了,它会得到下一个响铃时间,并继续执行 *,直到它被用户关闭为止 */ private void runCheck(){ Log.i(tag,“starting runCheck”);

Thread thread = new Thread() { 
     public void run() { 

      Log.i(tag, "starting LogoTimerThread"); 
      while (vRunningFlag) { 

       try { 

        // update the notification 
        makeNotification(); 

        Log.v(tag, 
          "Next Ring in : [" 
            + helper.TimeCalculator 
              .duration2_hh_MM_SS((vTimerNextRing - System 
                .currentTimeMillis())/1000) 
            + " sec.]"); 

        // check if time has run out 
        if (vTimerNextRing < System.currentTimeMillis()) { 
         // reset the timer 
         setNextRingTime(); 
         // update the screen 
         makeNotification(); 
         // play the sound 
         playLTimerSound(); 
        } 

        sleep(1000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 
      Log.i(tag, "finished LogoTimerThread"); 
     } 
    }; 

    thread.start(); 
} 

整个服务正在运行被设置为前景为远程服务,因此通知提醒服务的用户,他可以阻止它的方式。

一旦我打开playLTimerSound(),计时器就会倒计时。不知怎的,线程被播放声音停止。

这里IST该功能,以及:

public void playLTimerSound() { 
    Log.i(tag, "playLogoTimerSound - volume:" + vVolume); 
    MediaPlayer mp = MediaPlayer.create(this, R.raw.enterprise); 
    mp.setVolume(2.0f, 2.0f); 
    mp.start(); 
    mp.setOnCompletionListener(new OnCompletionListener() { 
     @Override 
     public void onCompletion(MediaPlayer mp) { 
      // TODO Auto-generated method stub 
      mp.release(); 
     } 
    }); 
} 

对不起,这里的格式,你可能有一些这方面的技巧,以及;-)

回答

0

不能AQUIRE SCREEN_DIM_WAKE_LOCK在你的情况?

@Override 
protected void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 




PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "App"); 
wl.acquire(); 

} 
+0

我是否必须在播放声音或创建之前获取锁定权限?我不太熟悉唤醒锁。 – user929995