2012-07-03 41 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

即使ü调用完成祝酒后,它的工作原理。我尝试过这个。

其实即使你不叫虚拟活动,它的作品。它只是你不会看到显示器亮度变化(明显),即使它正确地将亮度设置中的值。

如果您在背景中运行应用程序时转到设置>显示>亮度,则显示亮度的变化是可见的。

不知道这是为什么。