简体   繁体   English

Turn On/Off Android Device Mobile Data in C# Windows Form Application By Using Xamarin.Android

[英]Turn On/Off Android Device Mobile Data in C# Windows Form Application By Using Xamarin.Android

I have a Windows Form Application.我有一个 Windows 表格申请。 I want to change change Android device mobile data which is connected to the PC with USB.我想更改使用 USB 连接到 PC 的 Android 设备移动数据。

How can I change the state of Android device mobile data in Windows Form Application by using Xamarin.如何在 Windows 表单应用程序中使用 Z1B4419ABC43A358B41ZD0CA507 更改 Android 设备移动数据的 state

Is there any way to change the state of mobile data?有什么办法可以改变移动数据的state?

This should do it:这应该这样做:

void SetMobileDataEnabled(bool enabled)
{
    if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.L) {
        Console.WriteLine ("Device does not support mobile data toggling.");
        return;
    }

    try {
        if (Build.VERSION.SdkInt <= Android.OS.BuildVersionCodes.KitkatWatch 
            && Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Gingerbread) {
            Android.Net.ConnectivityManager conman = (Android.Net.ConnectivityManager)GetSystemService (ConnectivityService);
            Java.Lang.Class conmanClass = Java.Lang.Class.ForName (conman.Class.Name);
            Java.Lang.Reflect.Field iConnectivityManagerField = conmanClass.GetDeclaredField ("mService");
            iConnectivityManagerField.Accessible = true;
            Java.Lang.Object iConnectivityManager = iConnectivityManagerField.Get (conman);
            Java.Lang.Class iConnectivityManagerClass = Java.Lang.Class.ForName (iConnectivityManager.Class.Name);
            Java.Lang.Reflect.Method setMobileDataEnabledMethod = iConnectivityManagerClass.GetDeclaredMethod ("setMobileDataEnabled", Java.Lang.Boolean.Type);
            setMobileDataEnabledMethod.Accessible = true;

            setMobileDataEnabledMethod.Invoke (iConnectivityManager, enabled);
        }

        if (Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Gingerbread) {

            TelephonyManager tm = (TelephonyManager)GetSystemService (Context.TelephonyService);

            Java.Lang.Class telephonyClass = Java.Lang.Class.ForName (tm.Class.Name);
            Java.Lang.Reflect.Method getITelephonyMethod = telephonyClass.GetDeclaredMethod ("getITelephony");
            getITelephonyMethod.Accessible = true;

            Java.Lang.Object stub = getITelephonyMethod.Invoke (tm);
            Java.Lang.Class ITelephonyClass = Java.Lang.Class.ForName (stub.Class.Name);

            Java.Lang.Reflect.Method dataConnSwitchMethod = null;
            if (enabled) {
                dataConnSwitchMethod = ITelephonyClass
                    .GetDeclaredMethod ("disableDataConnectivity");
            } else {
                dataConnSwitchMethod = ITelephonyClass
                    .GetDeclaredMethod ("enableDataConnectivity");   
            }

            dataConnSwitchMethod.Accessible = true;
            dataConnSwitchMethod.Invoke (stub);
        } 
    } catch (Exception ex) {
        Console.WriteLine ("Device does not support mobile data toggling.");
    }
}

Enable the ChangeNetworkState and ModifyPhoneState permissions in your manifest.在清单中启用ChangeNetworkStateModifyPhoneState权限。

Android L currently has no available way to disable/enable mobile data. Android L 目前没有可用的方法来禁用/启用移动数据。

There is no need for (actually no use of using) Xamarin for this task.此任务不需要(实际上没有使用)Xamarin。 All you'll need is the Android Debug Bridge (ADB) (see the linked page for instructions on how to install it)您只需要Android 调试桥 (ADB) (有关如何安装它的说明,请参阅链接页面)

To enable and disable the mobile data connection, use the commands要启用和禁用移动数据连接,请使用命令

adb shell svc data enable
adb shell svc data disable

( see this answer , I've not marked this question as duplicate because it is scoped a bit differently) 见这个答案,我没有将此问题标记为重复,因为它的范围有点不同)

Please note that USB debugging has to be enabled on the device.请注意,必须在设备上启用 USB 调试。

In your windows application, you can implement the following class to en- or disable mobile data.在您的 windows 应用程序中,您可以实现以下 class 以启用或禁用移动数据。

class MobileDeviceService
{
    public void DisableMobileData()
    {
        Process.Start("adb.exe", "shell svc data disable");
    }

    public void EnableMobileData()
    {
        Process.Start("adb.exe", "shell svc data enable");
    }
}

If you'd like to hide the window or block until the command has finished, you can use Process.Start(ProcessStartInfo) with a StartInfo instance (see the docs for ProcessStartInfo ) that has been configured respectively.如果您想隐藏 window 或阻止命令完成,您可以将Process.Start(ProcessStartInfo)与已分别配置的StartInfo实例(请参阅ProcessStartInfo的文档)一起使用。

If there is more than one device attached to your PC, you can list the connected devices with如果有多个设备连接到您的 PC,您可以列出连接的设备

adb devices

and then use the -s <SERIAL> option, to select the respective device然后使用-s <SERIAL>选项,到 select 相应的设备

public void EnableMobileData(string deviceSerial)
{
    Process.Start("adb.exe", $"-s {deviceSerial} shell svc data enable");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM