Android-反射调用隐藏API

本文介绍了在Android开发中,如何使用反射技术来调用系统隐藏的API。通过一个具体的Java代码示例,详细展示了调用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    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.
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值