2

我想通过服务更改 android 设备的亮度。我已经看到很多关于堆栈溢出的问题,但没有一个能解决我的问题。

我已经完成了从在 settingsManger 中设置亮度、开始新活动等的所有事情,但没有一个对我有用。

4

4 回答 4

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();
    }
  }
于 2012-07-03T07:10:44.423 回答
0

即使您在敬酒后调用完成,它也有效。我尝试过这个。

实际上,即使您不调用虚拟活动,它也可以工作。只是您不会看到显示亮度发生变化(可见),即使它在设置中正确地设置了亮度值。

如果您在应用程序在后台运行时进入设置>显示>亮度,则显示亮度的变化是可见的。

虽然不知道为什么会这样。

于 2013-01-21T13:17:40.597 回答
0

I wanted same as you...

I also failed to find, Finally I had to implement...

Change brightness according to surrounding light in android

于 2013-01-25T07:19:20.147 回答
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();
}

}

于 2012-07-03T07:56:10.707 回答