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

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

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

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

测试手机miui13(android 12)

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"
        tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS"
        tools:ignore="ProtectedPermissions" />


    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Close"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值