123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package io.github.jianboy.wifiproxy;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import androidx.activity.EdgeToEdge;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.graphics.Insets;
- import androidx.core.view.ViewCompat;
- import androidx.core.view.WindowInsetsCompat;
- import io.github.jianboy.wifiproxy.utils.WifiProxyManager;
- public class MainActivity extends AppCompatActivity {
- Context mContext;
- Button btn1;
- EditText edt_host, edt_port;
- SharedPreferences sharedPreferences;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- EdgeToEdge.enable(this);
- setContentView(R.layout.activity_main);
- ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
- Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
- v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
- return insets;
- });
- mContext = this;
- initView();
- }
- private void initView() {
- btn1 = (Button) findViewById(R.id.button1);
- edt_host = (EditText) findViewById(R.id.edt_host);
- edt_port = (EditText) findViewById(R.id.edt_port);
- // 恢复设置
- sharedPreferences = mContext.getSharedPreferences("setting", Context.MODE_PRIVATE);
- String host = sharedPreferences.getString("host", getString(R.string.v_host));
- String port = sharedPreferences.getString("port", getString(R.string.v_port));
- edt_host.setText(host);
- edt_port.setText(port);
- btn1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- setWifiProxy();
- }
- });
- }
- /**
- * 设置wifi代理
- */
- private void setWifiProxy() {
- if (btn1.getText().toString().equals(R.string.set_wifi_proxy)) {
- WifiProxyManager.setWifiProxySettingsFor17And(mContext, edt_host.getText().toString().trim(),
- Integer.parseInt(edt_port.getText().toString().trim()), null);
- Toast.makeText(mContext, "设置成功!", Toast.LENGTH_SHORT).show();
- btn1.setText(R.string.cancel_wifi_proxy);
- } else {
- WifiProxyManager.unsetWifiProxySettingsFor17And(mContext);
- btn1.setText(R.string.set_wifi_proxy);
- }
- saveConfig();
- }
- /**
- * 保存设置,下次直接点击按钮即可设置代理
- */
- private void saveConfig() {
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString("host", edt_host.getText().toString().trim());
- editor.putString("port", edt_port.getText().toString().trim());
- editor.commit();
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (requestCode == WifiProxyManager.REQUEST_LOCATION_PERMISSION_CODE) {
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // Permission granted, proceed with accessing the location
- } else {
- // Permission denied, handle accordingly
- }
- }
- }
- }
|