一键关闭安卓手机wifi蓝牙定位

为了备用机极致的省电,在使用互传分享之后,要尽快关闭wifi蓝牙和定位。所以写个应用直接放桌面上,一键即可关闭。

使用的时候在系统设置里面把权限都打开。

直接上代码。里面注释掉的是尝试失败不好用的(magisk root手机的情况下,让app成为system app略麻烦,懒得弄了。)(然后bluetooth,如果直接使用shell命令,反而会弹出一个提示框:shell请求关闭蓝牙,我都申请了root权限还会这样,好奇怪。。)但是我用Bluetooth Adapter,在设置里给上权限,就可以直接无痛关闭了。

测试手机miui13(android 12)

AndroidManifest.xml(有些权限可以删掉,懒得调了。一股脑扔进来,不过bluetooth connect是一定需要的)




    
    
    
    
    
    
    
    
    


    
        
            
                

                
            
        
    

 MainActivity:

package com.kxd.closefun;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.content.pm.PackageManager;
import android.os.Bundle;

import android.content.Context;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.bluetooth.BluetoothAdapter;
import android.widget.Toast;

import java.io.DataOutputStream;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        // 获取WiFiManager并关闭WiFi
//        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
//        wifiManager.setWifiEnabled(false);

        // 获取BluetoothAdapter并关闭蓝牙
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            Toast.makeText(this, "Please Grant Bluetooth Permission First!", Toast.LENGTH_SHORT).show();
        }
        bluetoothAdapter.disable();
//
//        // 获取LocationManager并关闭所有的位置提供者
//        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//        for (String provider : locationManager.getProviders(true)) {
//            locationManager.removeTestProvider(provider);
//        }
        // 获取root权限
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());

            // 关闭WiFi
            os.writeBytes("svc wifi disable\n");//ok

            // 关闭蓝牙
            //os.writeBytes("am start -a android.bluetooth.adapter.action.REQUEST_DISABLE\n"); //会弹窗

            // 关闭GPS
            os.writeBytes("settings put secure location_mode 0\n"); // ok


            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            Toast.makeText(this, "Done.", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                assert process != null;
                process.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 退出程序
        finish();
    }
}

你可能感兴趣的:(android,智能手机,java)