2012-07-19 141 views
0

時,屏幕不會變暗當我從通知中調用某個活動時,我試圖讓屏幕變暗。這裏是代碼我使用:當我將它告訴

WindowManager.LayoutParams lp = getWindow().getAttributes(); 
lp.screenBrightness = 0.01f; 
getWindow().setAttributes(lp); 

也許我在這裏做一些錯誤,如果能這樣有人告訴我怎麼回事,爲什麼它不工作的方式它應該?!

import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 


public class NoteMe extends Activity { 
/** Called when the activity is first created. */ 
NotificationManager nm; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    String title = "Example text"; 
    String contentText = "Full hello world!"; 
    String text = "Starting Notification!"; 

    nm = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    long when = System.currentTimeMillis(); 
    int icon = R.drawable.ic_launcher; 

    Intent intent = new Intent(getBaseContext(), MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(
      getBaseContext(), 0, intent, 0); 

    Notification notification = new Notification(icon, text, when); 
    notification.setLatestEventInfo(getBaseContext(), title, contentText, 
      contentIntent); 
    notification.flags = Notification.FLAG_ONGOING_EVENT; 
    int NOTIFICATION_ID = 10; 
    nm.notify(NOTIFICATION_ID, notification); 
} 
} 

這裏是請求的代碼

+0

請張貼顯示解僱通知的代碼。 – 2012-07-19 18:54:40

+0

@CodeDroid我把它給你。 – linuxrox 2012-07-19 19:07:01

+0

好的。您還需要在調用亮度的MainActivity中放置日誌或調試斷點。查看它是否達到該代碼。您擁有的代碼將調整僅執行屏幕的亮度。 – 2012-07-20 00:57:51

回答

0

你的代碼是100%正確。有兩種可能性,第一種可能性最大:

1)活動沒有從通知中被調用。我相信你有一種特殊的方式,你從涉及未決意圖的通知中被解僱。我想這就是問題所在。只要確保在onCreate和onStart中添加一些斷點,看看它是否被調用。或者在你的代碼附近放一個日誌語句,看看它是否被調用。我猜它永遠不會被調用。

2)你正在調整0.1f的調整是不可見的變化。

請仔細閱讀這一點,只是讓正在接收確認通知:

http://www.vogella.com/articles/AndroidNotifications/article.html

一個區別我看到的是getContextBase()與此

如果它不是,它看起來像也許MainActivity。級艙單聲明可能是不正確的。

+0

請發佈您的顯示解僱通知的代碼 – 2012-07-19 18:54:23

+0

我發佈了有問題的代碼。 – linuxrox 2012-07-19 19:26:06

0

嘗試是這樣的 - 在刷新屏幕已設置的屬性之後。

WindowManager.LayoutParams lp = getWindow().getAttributes(); 
    lp.screenBrightness =0.01f 
    getWindow().setAttributes(lp); 

    startActivity(new Intent(this,RefreshScreen.class)); 

因此,一個刷新屏幕的黑客正在開始虛擬活動,而不是創建該虛擬活動來調用finish();所以亮度的變化纔會起作用。

0

您發佈的代碼只會讓您的應用程序變暗。如果這就是你想要的,那很好。但是如果你想調暗屏幕在手機上,那麼你需要更改系統設置System.SCREEN_BRIGHTNESS

// turn on manual control of system screen brightness 
    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 

    final int dimBrightnessValue = 96; // valid values 0-255 
    // change the brightness value 
    System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS, dimBrightnessValue); 

您還需要以下添加到AndroidManifest.xml中

<!-- Needed for changing screen brightness --> 
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 
+0

是的,如果你需要它在所有屏幕上變暗,這是正確的。有時候,您需要先在首選項中保存該值,然後再恢復。 – 2012-07-19 18:53:32

相關問題