通过编程方式(Android)的以太网连接(根设备) [英] Ethernet Connectivity through Programmatically (Android) (Rooted Device)

查看:469
本文介绍了通过编程方式(Android)的以太网连接(根设备)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于以太网,我遇到了一个小问题.

I have a small issue regarding Ethernet.

我的三个问题是:

  1. 我们可以以编程方式打开/关闭Ethernet吗?

我们可以通过编程方式启用/禁用Ethernet吗?

Can we programmatically Enable/Disable Ethernet?

我们可以以编程方式连接Ethernet吗?

Can we programmatically Connect Ethernet?

上述问题是通过Wifi完成的.喜欢

The above Questions are done with the Wifi. Like

  1. 我们可以通过编程方式打开/关闭Wifi.

我们可以通过编程方式启用/禁用Wifi.

We can programmatically Enable/Disable Wifi.

我们可以使用WifiManager以编程方式连接Wifi.

We can programmatically Connect Wifi using WifiManager.

android是否提供EthernetManager之类的EthernetManager来处理Ethernet?

Does android provides any EthernetManager like as WifiManager to handle Ethernet?

或者,如果这似乎不可行,那么我的原始要求是:

Or, if this doesn't seem feasible, then my original requirement is:

我要清除的第一件事是设备已扎根".

The first thing I am going to clear is "DEVICE IS ROOTED" .

我可以操纵设置(默认)吗?就像我不希望Settings.apkEthernet以外的Settings.apk中的任何其他选项.它应该仅显示WifiEthernet.而已.我可以从设置"中禁用所有选项,还是可以从设置"中删除所有其他选项?

Can I manipulate the Settings (Default)? Like I don't want any other option in the Settings.apk other than WIFI and Ethernet. It should show only Wifi and Ethernet. That's it. Can I disable all the options from the Settings or Can I remove all the other options from the Settings?

推荐答案

我将在此处介绍的解决方案是使用反射的hack,仅适用于扎根的android系统.

The solution I will present here is a hack using reflection and does only work on a rooted android system.

您的设备可能装有流行的android.net.ethernet软件包.在活动中,尝试

Your device might have the popular android.net.ethernet package. In an Activity, try

Object emInstance = getSystemService("ethernet");

它返回EthernetManager的有效实例,或者返回null.空表示您不走运.

It returns an valid instance of the EthernetManager or null. Null means you are out of luck.

一个附加的要求可能取决于您的设备:以太网和Wifi可能仅可单独使用.您可能需要禁用Wifi才能启用以太网,反之亦然.

An additional requirement might be depending on your device: Ethernet and Wifi might only work exclusively. You might need to disable Wifi to enable Ethernet and vice versa.

要通过反射启用以太网,请使用EthernetManager的实例. 您要调用的方法是setEthEnabled(启用布尔值)

To enable Ethernet by reflection use your instance of the EthernetManager. The method you want to invoke is setEthEnabled(boolean enabled)

    Class<?> emClass = null;
    try {
        emClass = Class.forName("android.net.ethernet.EthernetManager");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Object emInstance = getSystemService("ethernet");

    Method methodSetEthEnabled = null;
    try {
        methodSetEthEnabled = emClass.getMethod("setEthEnabled", Boolean.TYPE);
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    methodSetEthEnabled.setAccessible(true);
    try {
        // new Boolean(true) to enable, new Boolean(false) to disable
        methodSetEthEnabled.invoke(emInstance, new Boolean(false));
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

您的应用清单需要这些权限

Your application manifest needs these permissions

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

WRITE_SECURE_SETTINGS权限只能由系统应用程序获取.该应用无需通过系统密钥进行签名.它可以是任何有效的符号(例如常规的Android App Export功能).使用busybox重新挂载系统分区以进行写访问,然后将apk移到/system/app文件夹中.重新启动设备,它应该可以工作.

The permission WRITE_SECURE_SETTINGS can only be acquired by system apps. The app does not need to be signed by a system key. It can be any valid sign (like the regular Android App Export function). Use busybox to remount the system partition for write access and move your apk into the /system/app folder. Reboot the device and it should work.

我们可以以编程方式连接以太网吗?

Can we programmatically Connect Ethernet ?

没有接入点可以像Wifi一样连接您.您可以为DHCP配置它或提供静态值.当然,这也可以通过反射来完成. 为此,您将需要类EthernetDevInfo.

There is no Access Point to connect you like with Wifi. You either configure it for DHCP or provide static values. This can of course also be done via reflection. You will need the class EthernetDevInfo for that.

EthernetManager和EthernetDevInfo的实际实现在Android版本和设备之间可能会略有不同,因为它不必符合公共api(尚未),甚至可能是自定义版本. 要获取获取器和设置器的列表,可以使用 Introspector 或一般的反射.

The actual implementation of the EthernetManager and EthernetDevInfo might slightly differ between Android versions and devices as it doesn't have to conform to a public api (yet) and might even be a custom version. To get a list of getters and setters you can use a Introspector or reflection in general.

这篇关于通过编程方式(Android)的以太网连接(根设备)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆