Android-反射调用隐藏API
来源:互联网 发布:淘宝已购买的宝贝不见 编辑:程序博客网 时间:2024/04/30 04:32
android中有些时候我们在写代码时无法直接调用隐藏的API,这就需要用发射的方法来获得。下面通过一个java文件展示一个如何调用隐藏API的例子:
import android.app.Activity;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TetheringDemo extends Activity implements CompoundButton.OnCheckedChangeListener {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((ToggleButton) findViewById(R.id.toggle_tethering)).setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
try {
//获取WifiManager----WifiManager的setWifiApConfiguration方法是隐藏API,所以上层不能直接调用
//但是可以通过反射来间接调用
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
Boolean result = false;
WifiConfiguration config = new WifiConfiguration();
config.SSID = "EriksOpenWifi";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//初始化需要反射调用的方法名
String setWifiApConfigurationMethodName = "setWifiApConfiguration";
//Method 用来回调反射方法
Method setWifiApConfigurationMethod = wifiManager.getClass().getMethod(setWifiApConfigurationMethodName, WifiConfiguration.class);
//调用反射方法
result = (Boolean) setWifiApConfigurationMethod.invoke(wifiManager, config);
if (result) {
//初始化需要反射调用的方法名
String setWifiApEnableMethodName = "setWifiApEnabled";
//Method 用来回调反射方法
Method setWifiApEnableMethod = wifiManager.getClass().getMethod(setWifiApEnableMethodName, WifiConfiguration.class, boolean.class);
String message;
if (isChecked) {
result = (Boolean) setWifiApEnableMethod.invoke(wifiManager, null, true);
if (result) {
message = "Enabling tethering successfully";
} else {
message = "Enabling tethering failed";
}
} else {
result = (Boolean) setWifiApEnableMethod.invoke(wifiManager, null, false);
if (result) {
message = "Disabling tethering successfully";
} else {
message = "Disabling tethering failed";
}
}
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Failed to update Wifi Tethering config.", Toast.LENGTH_SHORT).show();
}
} catch (NoSuchMethodException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
- Android-反射调用隐藏API
- [Android] 利用java反射调用隐藏Api
- 采用反射调用android隐藏的api
- java反射机制 调用android得隐藏api
- android学习日记:利用反射调用隐藏API
- 如何调用Android隐藏API
- 如何调用Android隐藏API
- 如何调用Android隐藏API
- 如何调用Android隐藏API
- 如何调用Android隐藏API
- 如何调用Android隐藏API
- 利用Java反射技术调用Android中被隐藏的API
- Android 反射、代理调用系统隐藏API方法与接口类连接Wi-Fi
- android 反射invoke隐藏API(not verified)
- 利用反射调用android隐藏函数getVolumePaths
- Android开发之调用系统隐藏API
- Android开发 调用系统隐藏API
- Android开发-调用系统隐藏API
- JavaScript压缩工具JSA使用介绍
- 02-HTML-列表-框架
- Decoding简洁解决方案
- 讨论B树索引中的 is null/is not null
- web.xml 中的context-param 、listener、 filter、servlet 加载顺序及其详解
- Android-反射调用隐藏API
- app后端设计(1)--api(2014.01.31更新)
- animated-rotate
- codeforces 159D Palindrome pairs
- HDU 2209 翻纸牌游戏(双广||状态压缩)
- 开源社区
- 该符号在函数 _glutInit_ATEXIT_HACK@8 中被引用
- SpringMVC ajax JSON 中文乱码问题
- 我的博客网站设计