以太网连接通过编程方式(安卓)(root权限的设备) [英] Ethernet Connectivity through Programatically (Android) (Rooted Device)

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

问题描述

我有一个关于以太网一个小问题。

I have a small issue regarding Ethernet.

我的三个问题:

  1. 我们可以通过编程的开启/关闭以太网

我们可以通过编程启用/禁用以太网

Can we programmatically Enable/Disable Ethernet ?

我们能否通过编程连接以太网

Can we programmatically Connect Ethernet ?

上述问题完成的无线。像

The above Questions are done with the Wifi. Like

  1. 我们可以通过编程的开/关无线

我们可以通过编程方式启用/禁用无线

We can programmatically Enable/Disable Wifi.

我们可以通过编程连接无线使用 WifiManager

We can programmatically Connect Wifi using WifiManager.

请问Android提供任何 EthernetManager 等为 WifiManager 来处理以太网?任何帮助将是非常美联社preciable。我是一个迫切需要。

Does android provides any EthernetManager like as WifiManager to handle Ethernet? Any help would be highly appreciable. I am in a urgent need.

(OR)

如果这并不似乎是可行的,然后我原来的要求是:

If this doesn't seems feasible then my original requirement is:

首先我要明确是设备根。

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

我可以操作的设置(默认值)?就像我不希望任何其他选项, Settings.apk WIFI 其他以太网。它应该只显示无线以太网。而已。我可以禁止设置所有选项或我可以删除设置所有其他选项?

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?

请我在必要。任何帮助将是非常美联社preciated ..

Please I am in a need. Any help would be highly appreciated..

推荐答案

我将在这里present的解决方案是使用反射一个黑客并不会只对一个植根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或无效的有效实例。空意味着你的运气了。

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

这是额外的要求可能会根据您的设​​备:以太网和无线可能只工作完全。您可能需要关闭无线网络,来支持以太网,反之亦然。

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应用程序导出功能)。使用busybox的重新装入写访问系统分区和移动你的apk到/系统/应用程序文件夹中。重新启动设备,它应该工作。

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 ?

没有接入点来连接你喜欢无线上网。你要么将其配置为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的实际实现可能的Andr​​oid版本和设备之间略有不同,因为它不具有以符合公共API(还),甚至可能是一个定制版本。 为了获得getter和setter的列表,你可以使用内部检查或反射一般。

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.

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

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