ScreenOnActivity
public class ScreenOnActivity extends Activity {
private boolean screen;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.doSwitch(true);
}
public void switchScreen(View v) {
this.doSwitch(!this.screen);
Button btn = (Button) this.findViewById(R.id.btn);
btn.setText(this.screen ? "Off" : "On");
}
private void doSwitch(boolean flag) {
this.screen = flag;
if (flag) {
this.getWindow().addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
else {
this.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
}
main.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="switchScreen" android:text="Off" /> </LinearLayout>
相關文章
沒有留言:
張貼留言