我有2个活动,MainActivity和VideoPlayerActivity。在MainActivity中我有一个套接字,我可以使用telnet从我的PC连接到它。然后我可以写行,它将执行我发送给它的命令。例如,如果我写start
,它将启动VideoPlayerActivity并播放电影。
现在我想控制屏幕亮度。在尚未启动VideoPlayerActivity的MainActivity中,我可以轻松地在telnet中编写命令,如brightness=0.1
,并将MainActivity的亮度设置为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);
}
}
现在的问题是,当VideoActivity启动时,它会忽略预设亮度并使用系统定义的亮度。所以我把这个方法放在VideoActivity中:
public void setBrightnessLevel(float value)
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = value;
getWindow().setAttributes(lp);
}
但是一旦我写命令改变亮度,整个应用程序就会停止。由于我在上面的第一个代码中的这一部分提出了问题:
if(_videoPlayerIntent != null && _videoPlayerActivity.isActive)
{
Log.d("CALLING VIDEOACTIVITY", "SET BRIGHTNESS");
_videoPlayerActivity.setBrightnessLevel(BrightnessValue);
}
你能告诉我如何处理这种情况?我需要能够在VideoActivity运行时更改屏幕亮度,并且我的套接字在MainActivity中...
这是VideoActivity中的方法....我试图将其设为静态然后问题是如果方法是静态的,我无法访问getWindow()
:
public void setBrightnessLevel(float value)
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = value;
getWindow().setAttributes(lp);
}
答案 0 :(得分:0)
你需要第二个Activity
的句柄来设置它的窗口亮度,所以我建议你制作一个模型来告诉第一个Activity
的活动创建和破坏。
以下是监听活动状态的模型:
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();
}
}
您需要在OnActivityStateChangedListener
中实施MainActivity
界面,并将其设置为收听更改:
public class MainActivity extends Activity implements
OnActivityStateChangedListener {
// in onCreate(...)
ActivityStateListenerModel.getInstance().setListener(this);
然后回调,在我们设置标记的那些标记我们需要检查知道的是仍在运行的活动:
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;
}
当您的套接字读取数据时,您只需在MainActivity
:
if(mOtherActivity != null && mOtherActivityStarted) {
SecondActivity.setBrightnessLevel(brightnessValue, mOtherActivity);
}
然后你有其他Activity
(VideoActivity),所以你需要通知模型创建或销毁活动:
// in onCreate(...)
// This gives the handle to MainActivity
ActivityStateListenerModel.getInstance().activityStarted(this);
// in onDestroy()
ActivityStateListenerModel.getInstance().activityDestroyed();
从VideoActivity 更改brightess的方法(在这种情况下,此方法可以在任何地方,因为我们在参数中传递活动):
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);
}
}
并且从小写字母开始命名变量也是一个好习惯......
(BrightnessValue => brightnessValue)