如何创建用于开启和关闭“保持屏幕常亮”的设置。

huangapple go评论83阅读模式
英文:

How to create Settings for Switching ON and OFF of 'keep_screen_ON'

问题

这是您要求的翻译部分:

我一直在努力创建一个用于FLAG_KEEP_SCREEN_ON的CheckBox Switch,以便切换屏幕显示的开关。我已经成功创建了它,但它只在一个活动中有效。我想将按钮放在MainActivity中,这样用户可以更轻松地决定是保持屏幕开启还是关闭。我希望它成为整个应用程序中唯一的控制开关。

请任何帮助将不胜感激。

这是我所做的,但在所有活动中不起作用。

这是XML部分

<RelativeLayout
    android:id="@+id/mainlayout"
    android:background="@drawable/layout_bg2"
    android:layout_width="match_parent"
    android:layout_height="30sp">

    <Switch
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="50dp"
        android:id="@+id/ScreenController"/>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="保持屏幕关闭"
        android:layout_centerHorizontal="true"
        android:background="@drawable/layout_bg2"
        android:layout_marginTop="2dp"
        android:layout_toRightOf="@id/ScreenController"
        android:layout_marginLeft="30dp"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:fontFamily="serif"
        android:id="@+id/StatusText"/>
</RelativeLayout>

这是Java代码部分

MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if(isChecked){

            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            String currentText = MySwitch.getText().toString();
            MyText.setText(currentText.equals("保持屏幕开启") ? "保持屏幕关闭" : "保持屏幕开启");
            MyText.setTextColor(Color.parseColor("#FFEB3B"));

        }else{

            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

            String currentText = MySwitch.getText().toString();
            MyText.setText(currentText.equals("保持屏幕关闭") ? "保持屏幕开启" : "保持屏幕关闭");
            MyText.setTextColor(Color.parseColor("#000000"));

        }
    }
});
英文:

I have been struggling to create a CheckBox Switch for FLAG_KEEP_SCREEN_ON that can switch ON/OFF the screen display. I have managed to create it but it only works for one activity. I want to place the button maybe in the MainActivity where it will be easier for a user to quickly decide to either keep the screen ON/OFF. I want it to be one and only control switch for the entire activities in the app.
Please any help will be much appreciated.

This is what i did that doesn't work as a control settings in all activities.

This is the XML

&lt;RelativeLayout
       android:id=&quot;@+id/mainlayout&quot;
        android:background=&quot;@drawable/layout_bg2&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;30sp&quot;&gt;



    &lt;Switch
        android:layout_height=&quot;wrap_content&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_alignParentTop=&quot;true&quot;
        android:layout_marginTop=&quot;2dp&quot;

       android:layout_marginLeft=&quot;50dp&quot;
        android:id=&quot;@+id/ScreenController&quot;/&gt;

    &lt;TextView
        android:layout_height=&quot;wrap_content&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:text=&quot;Keeps Screen OFF&quot;
        android:layout_centerHorizontal=&quot;true&quot;
        android:background=&quot;@drawable/layout_bg2&quot;
        android:layout_marginTop=&quot;2dp&quot;
        android:layout_toRightOf=&quot;@id/ScreenController&quot;
        android:layout_marginLeft=&quot;30dp&quot;
        android:textSize=&quot;16sp&quot;
        android:textStyle=&quot;bold&quot;
        android:textColor=&quot;#000000&quot;
        android:fontFamily=&quot;serif&quot;
        android:id=&quot;@+id/StatusText&quot;/&gt;

    &lt;/RelativeLayout&gt;

This is the Java code

MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {

                if(isChecked){

                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    String currentText = MySwitch.getText().toString();
                    MyText.setText(currentText.equals(&quot;Screen Light Stays ON&quot;) ? &quot;Screen Light Stays OFF&quot; : &quot;Screen Light Stays ON&quot;);
                    MyText.setTextColor(Color.parseColor(&quot;#FFEB3B&quot;));

                }else{

                    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

                    String currentText = MySwitch.getText().toString();
                    MyText.setText(currentText.equals(&quot;Screen Light Stays OFF&quot;) ? &quot;Screen Light Stays ON&quot; : &quot;Screen Light Stays OFF&quot;);
                    MyText.setTextColor(Color.parseColor(&quot;#000000&quot;));


                }
            }
        });

答案1

得分: 1

保存您的变量在共享偏好中,然后创建一个检查该变量并设置标志的活动。
然后,您只需在所有的活动中扩展此活动。

public class Main2Activity extends AppCompatActivity {
    String shared_prefs_name = "MY_prefs_name";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (screen_on()) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }

    public void set_screen_on(boolean screen_on) {
        SharedPreferences.Editor saver = getSharedPreferences(shared_prefs_name, MODE_PRIVATE).edit();

        saver.putBoolean("screen_on", screen_on);
        saver.commit();
        
        if (screen_on) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }

    public boolean screen_on() {
        SharedPreferences prefs = getSharedPreferences(shared_prefs_name, MODE_PRIVATE);
        return prefs.getBoolean("screen_on", false);
    }
}

这样,您可以在您的活动中使用它,像这样:

public class Main3Activity extends Main2Activity {
    Switch MySwitch;
    TextView MyText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        MySwitch = (Switch) findViewById(R.id.ScreenController);
        MyText = (TextView) findViewById(R.id.StatusText);

        MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                set_screen_on(isChecked);
                if (isChecked) {
                    String currentText = MySwitch.getText().toString();
                    MyText.setText(currentText.equals("Screen Light Stays ON") ? "Screen Light Stays OFF" : "Screen Light Stays ON");
                    MyText.setTextColor(Color.parseColor("#FFEB3B"));
                } else {
                    String currentText = MySwitch.getText().toString();
                    MyText.setText(currentText.equals("Screen Light Stays OFF") ? "Screen Light Stays ON" : "Screen Light Stays OFF");
                    MyText.setTextColor(Color.parseColor("#000000"));
                }
            }
        });

        if (screen_on()) {
            String currentText = MySwitch.getText().toString();
            MyText.setText(currentText.equals("Screen Light Stays ON") ? "Screen Light Stays OFF" : "Screen Light Stays ON");
            MyText.setTextColor(Color.parseColor("#FFEB3B"));
        } else {
            String currentText = MySwitch.getText().toString();
            MyText.setText(currentText.equals("Screen Light Stays OFF") ? "Screen Light Stays ON" : "Screen Light Stays OFF");
            MyText.setTextColor(Color.parseColor("#000000"));
        }
    }
}

主要是扩展Main2Activity以获得对screen_on()布尔值的访问,并使用set_screen_on(boolean)进行设置。

英文:

save your variable in shared preference then have an activity that checks the variable and sets the flag.
you will then just extend this activity in all your activities.

 public class Main2Activity extends AppCompatActivity {
String shared_prefs_name=&quot;MY_prefs_name&quot;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(screen_on()){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}else{
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
public void set_screen_on( boolean screen_on) {
SharedPreferences.Editor saver = getSharedPreferences(shared_prefs_name, MODE_PRIVATE).edit();
saver.putBoolean(&quot;screen_on&quot;, screen_on);
saver.commit();
if(screen_on)
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}else{
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
public boolean screen_on() {
SharedPreferences prefs = getSharedPreferences(shared_prefs_name, MODE_PRIVATE);
return prefs.getBoolean(&quot;screen_on&quot;, false);
}
}

So you can use it in you activity like so .

public class Main3Activity extends Main2Activity {
Switch MySwitch;
TextView MyText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
MySwitch=(Switch)findViewById(R.id.ScreenController);
MyText=(TextView) findViewById(R.id.StatusText);
MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
set_screen_on(isChecked);
if(isChecked){
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals(&quot;Screen Light Stays ON&quot;) ? &quot;Screen Light Stays OFF&quot; : &quot;Screen Light Stays ON&quot;);
MyText.setTextColor(Color.parseColor(&quot;#FFEB3B&quot;));
}else{
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals(&quot;Screen Light Stays OFF&quot;) ? &quot;Screen Light Stays ON&quot; : &quot;Screen Light Stays OFF&quot;);
MyText.setTextColor(Color.parseColor(&quot;#000000&quot;));
}
}
});
if(screen_on())
{
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals(&quot;Screen Light Stays ON&quot;) ? &quot;Screen Light Stays OFF&quot; : &quot;Screen Light Stays ON&quot;);
MyText.setTextColor(Color.parseColor(&quot;#FFEB3B&quot;));
}else{
String currentText = MySwitch.getText().toString();
MyText.setText(currentText.equals(&quot;Screen Light Stays OFF&quot;) ? &quot;Screen Light Stays ON&quot; : &quot;Screen Light Stays OFF&quot;);
MyText.setTextColor(Color.parseColor(&quot;#000000&quot;));
}
}
}

Main thing is to extend Main2Activity to get accees to the boolean screen_on() and to set with set_screen_on(boolean);

huangapple
  • 本文由 发表于 2020年8月6日 18:23:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63281544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定