2012-07-03 63 views
2

我想更改服務的Android設備亮度。我在堆棧溢出上看到了很多問題,但沒有一個解決了我的問題。從服務中更改亮度android

我已經完成了設置settingsManger中的亮度,開始一個新的活動等 ,但他們都沒有爲我工作。

回答

1

嘗試爲

android.provider.Settings.System.putInt(getContentResolver(), 
    android.provider.Settings.System.SCREEN_BRIGHTNESS, 
    BRIGHTNESS_Value); 

和添加的Manifest.xml許可

<uses-permission android:name="android.permission.WRITE_SETTINGS"/> 
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> 
</manifest> 

編輯: 嘗試爲:

public class ExampleService extends Service { 

    @Override 
    public void onCreate() { 
     // The service is being created 
     // set SCREEN_BRIGHTNESS 
     android.provider.Settings.System.putInt(getContentResolver(), 
     android.provider.Settings.System.SCREEN_BRIGHTNESS, 
     BRIGHTNESS_Value); 
     /// start new Activity 
     Intent intent = new Intent(getBaseContext(), ExampleActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     getApplication().startActivity(intent); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // A client is binding to the service with bindService() 
     return mBinder; 
    } 
} 

public class ExampleActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // The activity is being created. 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     // The activity has become visible (it is now "resumed"). 
     this.finish(); 
    } 
    } 
+0

我已經做到了,但問題是這種變化是唯一當我開始任何其他事情時就會應用,比如啓動通知管理器。它不會自動應用。 –

+0

嘗試添加.CHANGE_CONFIGURATION權限 –

+0

不工作..同樣的問題 –

0

找到答案: 創建一個無形的活動,並不要完成()它。 使所有在活動

代碼虛活動的變化:

public class DummyActivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dummy_layout); 
    int progress = getIntent().getIntExtra("brightness", 0); 
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); 
    layoutParams.screenBrightness = progress/255.0f; 
    getWindow().setAttributes(layoutParams); 


    Toast.makeText(this, "Came in Dummy Activity", Toast.LENGTH_SHORT).show(); 
} 
@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
} 

}

+0

然後如何將用戶點擊其他應用程序,並在主屏幕上? –

+0

我只是在製作一個數據收集應用程序。現在我不關心這一點。但是你的觀點非常重要,如果有人想把這個功能包含到一個完整的應用程序中 –

+0

請參閱我的編輯答案 –

0

即使ü調用完成祝酒後,它的工作原理。我嘗試過這個。

其實即使你不叫虛擬活動,它的作品。它只是你不會看到顯示器亮度變化(明顯),即使它正確地將亮度設置中的值。

如果您在背景中運行應用程序時轉到設置>顯示>亮度,則顯示亮度的變化是可見的。

不知道這是爲什麼。