2013-04-22 58 views
0

當我的應用程序收到通知時,需要打開屏幕背光。 我聽到聲音,LED指示燈亮起,但屏幕仍保持關閉狀態。當我的應用程序收到通知時打開屏幕背光

我試着使用PowerManager:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); 
wl.acquire(); 
..screen will stay on during this section.. 
wl.release(); 

但沒有結果。

通知代碼如下所示:

public void showNotification(String contentTitle, String contentText) { 
    int icon = R.drawable.notification; 
    long when = System.currentTimeMillis(); 



    Notification notification = new Notification(icon, contentTitle, when); 


    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.defaults |= Notification.DEFAULT_LIGHTS; 




    Intent notificationIntent = new Intent(this, myapp.class); 
    Context context = this.getApplicationContext(); 


    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); 

    NotificationManager nm = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); 




    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
    v.vibrate(300); 

    notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sonar_ping); 

    notification.flags |= Notification.FLAG_SHOW_LIGHTS; 
    notification.ledARGB = 0x00000000; 
    notification.ledOnMS = 0; 
    notification.ledOffMS = 0; 

    nm.notify(1, notification); 




} 

如何打開時,屏幕上的通知顯示在狀態欄? 我需要在這段代碼中添加什麼來實現它?

感謝您的任何建議和幫助。

+1

[以編程方式打開屏幕]的可能的重複(http://stackoverflow.com/questions/2891337/turning-on-screen-programmatically) – WarrenFaith 2013-04-22 12:59:19

回答

4

試試這個

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | 
      PowerManager.ACQUIRE_CAUSES_WAKEUP | 
      PowerManager.ON_AFTER_RELEASE, "WakeLock"); 
    wakeLock.acquire(); 

和`噸忘記釋放它。

+0

謝謝。這是我正在尋找的解決方案。 – Sergio 2013-04-22 14:16:29

+0

你的歡迎:) – 2013-04-22 14:21:09

相關問題