I have 2 Activities, MainActivity and VideoPlayerActivity. In the MainActivity I have a socket that I can connect to it from my PC using telnet. Then I can write line and it will execute the commands I send to it. for example if I write start
it will start VideoPlayerActivity and plays a movie.
Now I want to control the screen brightness. In the MainActivity when the VideoPlayerActivity is not started yet, I can easily write a command in telnet like brightness=0.1
and that will set the brightness of the MainActivity to 0.1:
if(msg.startsWith("brightness="))
{
String brightText = msg.substring(msg.indexOf('=') + 1, msg.length());
BrightnessValue = Float.parseFloat(brightText);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = BrightnessValue;
getWindow().setAttributes(lp);
if(_videoPlayerIntent != null && _videoPlayerActivity.isActive)
{
_videoPlayerActivity.setBrightnessLevel(BrightnessValue);
}
}
Now the problem is, when VideoActivity starts, it ignores the preset brightness and will use the system defined brightness. So I put this method in VideoActivity :
public void setBrightnessLevel(float value)
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = value;
getWindow().setAttributes(lp);
}
but as soon as I write command to change brightness the whole app stops. Because of this section in the first code I put above in the question:
if(_videoPlayerIntent != null && _videoPlayerActivity.isActive)
{
Log.d("CALLING VIDEOACTIVITY", "SET BRIGHTNESS");
_videoPlayerActivity.setBrightnessLevel(BrightnessValue);
}
Can you tell me how can I handle this situation? I need to be able to change brightness of screen when the VideoActivity is running, and my socket is in MainActivity...
This is the method in VideoActivity....I tried to make it static then the problem is I can not access getWindow()
if the method is static:
public void setBrightnessLevel(float value)
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = value;
getWindow().setAttributes(lp);
}
You need a handle to that second
Activity
to set it's window brightness so i suggest you to make a model that tells the activity creation and destruction to the firstActivity
.Here is the model for listening the activitys state:
And you need to implement
OnActivityStateChangedListener
interface in yourMainActivity
and set it to listen the changes:Then the callbacks, in those we set flag what we need to check to know is the activity still running:
And when your socket reads data you just do this in your
MainActivity
:Then you have that other
Activity
(VideoActivity), so there you need to notify the model that activity is created or destroyed:And the method that changes the brightess from VideoActivity (in this case this method can be anywhere because we pass the activity in the parameters):
and also it's a good habit to name your variables starting with lower case letter...
(BrightnessValue => brightnessValue)