如何在 Android 上禁用移动数据?
此示例演示了如何在 Android 中禁用移动数据。
为了查找信息,除非您拥有已root的手机,否则我认为您无法以编程方式启用和禁用数据,因为要做到这一点,我们必须包含MODIFY_PHONE_STATE权限,此权限仅授予系统或签名应用程序。
setMobileDataEnabled() 方法不再可以通过反射调用。从 Android 2.1(API 7)到 Android 4.4(API 19),它可以通过反射调用,但从 Android 5.0 及更高版本开始,即使在已root的手机上,setMobileDataEnabled() 方法也无法调用。
步骤 1 - 在 Android Studio 中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建新项目。
步骤 2 - 将以下代码添加到 res/layout/activity_main.xml 中。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" tools:context=".MainActivity"> <Switch android:id="@+id/switchData" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Mobile Data" /> </LinearLayout>
步骤 3 - 将以下代码添加到 src/MainActivity.java 中。
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
import java.lang.reflect.Method;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
Switch mySwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySwitch = findViewById(R.id.switchData);
mySwitch.setChecked(getMobileDataState());
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setMobileDataState(isChecked);
}
});
}
public void setMobileDataState(boolean mobileDataEnabled) {
try {
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method setMobileDataEnabledMethod = Objects.requireNonNull(telephonyService).getClass().getDeclaredMethod("setDataEnabled", boolean.class);
setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
} catch (Exception ex) {
Log.e("MainActivity", "Error setting mobile data state", ex);
}
}
public boolean getMobileDataState() {
try {
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method getMobileDataEnabledMethod = Objects.requireNonNull(telephonyService).getClass().getDeclaredMethod("getDataEnabled");
return (boolean) (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
} catch (Exception ex) {
Log.e("MainActivity", "Error getting mobile data state", ex);
}
return false;
}
}步骤 4 - 将以下代码添加到 androidManifest.xml 中。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行您的应用程序。我假设您已将您的实际 Android 移动设备连接到您的计算机。要从 Android Studio 运行应用程序,请打开项目中的一个活动文件,然后单击工具栏中的运行
图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 -

点击此处下载项目代码
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP