changing brightness from Service android

陌路散爱 提交于 2019-12-11 02:19:57

问题


I want to change brightness of android device from a service. I have seen a lot of questions on stack overflow on this but none of them has solved my problem.

I have done every thing from setting brightness in settingsManger, starting a new activity etc but none of them is working for me.


回答1:


try as

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

and add permission in Manifest.xml

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

Edit: try as:

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();
    }
  }



回答2:


Found answer : Create an invisible activity, and do not finish() it. Make all the changes in activity

Code for dummy activity:

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();
}

}




回答3:


Even if u call finish after the toast, it works. I tried it.

Actually even if u do not call the dummy activity, it works. Its just that you wont see the display brightness changing(visibly), even though it is putting the value correctly in brightness in settings.

If u go Settings>Display>Brightness while your app is running in the background, the change in display brightness is visible.

Don't know why is this though.




回答4:


I wanted same as you...

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

Change brightness according to surrounding light in android



来源:https://stackoverflow.com/questions/11306049/changing-brightness-from-service-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!