lzw
2024-11-04 dd257d5d6ab766917c1fea03cf109b07797169c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.yw.sdkdemo.airplane;
 
import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
 
import com.yw.sdkdemo.R;
import com.yw.serialport.Port;
import com.yw.serialport.SerialPortManager;
import com.yw.tool.ToolsManager;
 
 
/**
 * Created by xushuang on 2024/5/10
 * E-mail: mitgic92@gmail.com
 * Description:
 */
public class AirPlaneActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt_open;
    private Button bt_close;
    private static final String TAG = AirPlaneActivity.class.getSimpleName();
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_airplane);
 
        initView();
        initListener();
    }
 
    private void initView() {
        bt_open = (Button) findViewById(R.id.bt_open);
        bt_close = (Button) findViewById(R.id.bt_close);
    }
 
    private SerialPortManager.Listener listener = new SerialPortManager.Listener() {
        @Override
        public void onData(Port port, byte[] data, int len) throws RemoteException {
            Log.d(TAG, "onData: " + new String(data));
        }
    };
 
    private void initListener() {
        bt_open.setOnClickListener(this);
        bt_close.setOnClickListener(this);
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
 
    private boolean isAirplaneModeOn = false;
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_open:
                setAirplaneMode(true);
                break;
            case R.id.bt_close:
                setAirplaneMode(false);
                break;
 
 
        }
    }
 
    public void setAirplaneMode(boolean enabled) {
        new Thread() {
            @Override
            public void run() {
                isAirplaneModeOn = isAirplaneModeOn(AirPlaneActivity.this);
                Log.d(TAG, "Airplane mode is " + (isAirplaneModeOn ? "ON" : "OFF"));
                ToolsManager.getInstance().switchAirPlaneMode(enabled);
                isAirplaneModeOn = isAirplaneModeOn(AirPlaneActivity.this);
                Log.d(TAG, "Airplane mode is " + (isAirplaneModeOn ? "ON" : "OFF"));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(AirPlaneActivity.this, "Airplane mode is " + (isAirplaneModeOn ? "ON" : "OFF"),Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }.start();
 
    }
 
 
    public boolean isAirplaneModeOn(Context context) {
        return Settings.Global.getInt(
                context.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
    }
 
}