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 first Activity
.
Here is the model for listening the activitys state:
public class ActivityStateListenerModel {
public interface OnActivityStateChangedListener {
void activityStarted(Activity activity);
void activityDestroyed();
}
private static ActivityStateListenerModel mInstance;
private OnActivityStateChangedListener mListener;
public static ActivityStateListenerModel getInstance() {
if(mInstance == null) {
mInstance = new ActivityStateListenerModel();
}
return mInstance;
}
public void setListener(OnActivityStateChangedListener listener) {
mListener = listener;
}
public void activityStarted(Activity activity) {
mListener.activityStarted(activity);
}
public void activityDestroyed() {
mListener.activityDestroyed();
}
}
And you need to implement OnActivityStateChangedListener
interface in your MainActivity
and set it to listen the changes:
public class MainActivity extends Activity implements
OnActivityStateChangedListener {
// in onCreate(...)
ActivityStateListenerModel.getInstance().setListener(this);
Then the callbacks, in those we set flag what we need to check to know is the activity still running:
private static boolean mOtherActivityStarted;
private static Activity mOtherActivity;
@Override
public void activityStarted(Activity activity) {
Log.d(TAG, "Second activity created");
mOtherActivityStarted = true;
mOtherActivity = activity;
}
@Override
public void activityDestroyed() {
Log.d(TAG, "Second activity destroyed");
mOtherActivityStarted = false;
mOtherActivity = null;
}
And when your socket reads data you just do this in your MainActivity
:
if(mOtherActivity != null && mOtherActivityStarted) {
SecondActivity.setBrightnessLevel(brightnessValue, mOtherActivity);
}
Then you have that other Activity
(VideoActivity), so there you need to notify the model that activity is created or destroyed:
// in onCreate(...)
// This gives the handle to MainActivity
ActivityStateListenerModel.getInstance().activityStarted(this);
// in onDestroy()
ActivityStateListenerModel.getInstance().activityDestroyed();
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):
static public void setBrightnessLevel(float value, Activity activity)
{
Window window = activity.getWindow();
if(window != null) {
Log.d(MainActivity.TAG, "Setting brightness to " + activity.getClass().getName());
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = value;
window.setAttributes(lp);
}
}
and also it's a good habit to name your variables starting with lower case letter...
(BrightnessValue => brightnessValue)