Android-反射调用隐藏API

来源:互联网 发布:淘宝已购买的宝贝不见 编辑:程序博客网 时间:2024/04/30 04:32

    android中有些时候我们在写代码时无法直接调用隐藏的API,这就需要用发射的方法来获得。下面通过一个java文件展示一个如何调用隐藏API的例子:


package com.sonyericsson.tetheringdemo;
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.
        }
    }
}
原创粉丝点击