android-如何向系统VPN设置页面添加自己的VPN设置?

android-如何向系统VPN设置页面添加自己的VPN设置?,第1张

概述有VPN的系统设置.我将可能基于VpnService类添加其他VPN服务.如我所见,有一个方法setConfigureIntent看起来与我需要的东西相似,但是我看不到任何用法示例.publicVpnService.BuildersetConfigureIntent(PendingIntentintent)AddedinAPIlevel14SetthePendingIntent

有VPN的系统设置.我将可能基于VpnService类添加其他VPN服务.如我所见,有一个方法setConfigureIntent看起来与我需要的东西相似,但是我看不到任何用法示例.

public VpnService.Builder setConfigureIntent (PendingIntent intent)

Added in API level 14
Set the PendingIntent to an activity for users to configure the VPN connection. If it is not set, the button to configure will not be shown in system-managed dialogs.

VPN设置页面在这里:

,.

实际上,我只需要向系统VPN设置添加一个按钮,然后单击显示带有VPN特定设置的自定义对话框即可.

解决方法:

这是使用Java Reflection提出的@shoe rat的起点:

package com.nikola.despotoski.whatever;import java.lang.reflect.Constructor;import java.lang.reflect.FIEld;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class VpnSetter {    private static Map<String , Class<?>> getMappedFIElds(){        Map<String , Class<?>> fIEldsAndTypes = new HashMap<String, Class<?>>();        fIEldsAndTypes.put("name", String.class);        // 0        fIEldsAndTypes.put("type" , int.class);   // 1        fIEldsAndTypes.put("server", String.class);        // 2        fIEldsAndTypes.put("username", String.class);        fIEldsAndTypes.put("password", String.class);        fIEldsAndTypes.put("dnsServers", String.class);        fIEldsAndTypes.put("searchDomains", String.class);        fIEldsAndTypes.put("routes", String.class);        fIEldsAndTypes.put("mppe", boolean.class);        fIEldsAndTypes.put("l2tpSecret", String.class);        fIEldsAndTypes.put("ipsecIDentifIEr", String.class);        fIEldsAndTypes.put("ipsecSecret", String.class);        fIEldsAndTypes.put("ipsecUserCert", String.class);        fIEldsAndTypes.put("ipsecCaCert", String.class);        fIEldsAndTypes.put("saveLogin", boolean.class);        return fIEldsAndTypes;    }    public static final Set<String> VPN_PROfile_KEYS = getMappedFIElds().keySet(); // contains keys for quicker generation of key-value map for each     public static voID addVpnProfile(String vpnProfileKey, Map<String, Object> values) throws ClassNotFoundException, NoSuchFIEldException, IllegalArgumentException, illegalaccessexception, InvocationTargetException, InstantiationException, NoSuchMethodException{        Class<?> vpnSettings = Class.forname("com.androID.settings.vpn2.VpnSettings");        Class<?>[] privateVpnSettingsClasses = vpnSettings.getDeclaredClasses();        Class<?> vpnPreference = null;        Class<?> vpnProfileClass = Class.forname("com.androID.settings.vpn2.VpnProfile");        for(Class<?> priv :privateVpnSettingsClasses ){            if(priv.getname().equals("VpnPreference")){                vpnPreference = priv;                break;            }        }        FIEld vpnProfileFromVpnPreferenceFIEld = vpnPreference.getDeclaredFIEld("mProfile");        vpnProfileFromVpnPreferenceFIEld.setAccessible(true);        Object vpnProfile = vpnProfileFromVpnPreferenceFIEld.get(vpnProfileClass);        Constructor<?> constructor = vpnProfileFromVpnPreferenceFIEld.getClass().getConstructors()[0];        constructor.setAccessible(true);        vpnProfile = constructor.newInstance(vpnProfileKey);//creating new instance of VpnProfile class        Map<String, Class<?>> vpnProfileMap = getMappedFIElds();        Iterator<String> profileKeysIterator = vpnProfileMap.keySet().iterator();        while(profileKeysIterator.hasNext()){            String key = profileKeysIterator.next();            FIEld fIEld = vpnProfile.getClass().getFIEld(key);            fIEld.setAccessible(true);            if(vpnProfileMap.get(key).equals(String.class) && values.get(key)!=null){                String s = new String();                fIEld.set(s, "value");//change this            }else if(vpnProfileMap.get(key).equals(boolean.class) && values.get(key)!=null){                int i = 0;                fIEld.setInt(i, 1111111);// change this            }else if(values.get(key)!=null){                boolean  b = false;                fIEld.setBoolean(b, true);// change this            }        }        vpnSettings = Class.forname("com.androID.settings.vpn.VpnSettings"); //time to add it to settings        Method addProfileMethod = vpnSettings.getDeclaredMethod("addProfile", vpnProfile.getClass());         addProfileMethod.setAccessible(true);        addProfileMethod.invoke(vpnSettings, vpnProfile);    }}

我没有时间测试它,但是可以肯定的是它将为您提供一个起点.我不确定访问此隐藏的API是否需要root用户访问权限.请记住,这可能会引发一些SecurityException或illegalaccessexception.

总结

以上是内存溢出为你收集整理的android-如何向系统VPN设置页面添加自己的VPN设置?全部内容,希望文章能够帮你解决android-如何向系统VPN设置页面添加自己的VPN设置?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1076170.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存