【发布时间】:2015-02-22 22:31:56
【问题描述】:
我想知道是否有人可以对有关 WifiP2pDevice 的名称字段的问题有所了解。
您可能已经知道,WifiP2pDevice 有一个设备名称字段,但没有“设置”设备名称的方法,只能读取它。
如本文所述: How change the device name in WiFi direct p2p?
可以通过反射设置设备的名称。我知道这是不好的习惯,但由于 API 不支持此功能并且我的应用程序中需要此功能,我该怎么做呢?
到目前为止,我的代码:
try
{
method = myManager.getClass().getMethod("setDeviceName", new Class[] { WifiP2pManager.Channel.class, String.class,
WifiP2pManager.ActionListener.class });
method.invoke(myManager, myChannel, getIntent().getStringExtra(NETWORK_NAME));
Toast.makeText(this, "Name set successful", Toast.LENGTH_SHORT).show();
}
当前,应用程序到达这一点并返回 NoSuchMethodException,暗示 setDeviceName 不存在。关于 java 反射的任何帮助/替代方法?
编辑 1:根据建议,我已将代码更改为使用 WifiP2pManager 而不是 WifiP2pDevice 来执行 getClass() 调用,但是,程序在 method.invoke 行崩溃,声称 setDeviceName 方法期望3 个争论,但只收到 2 个(即使现在的模式代码正在发送 3 个)。
【问题讨论】:
标签: java android reflection p2p wifip2p