gpt4 book ai didi

java - android.net.VPNServicec 不是封闭类

转载 作者:太空宇宙 更新时间:2023-11-04 09:09:35 26 4
gpt4 key购买 nike

尝试按照android文档实现VPN并面临这个问题

我当前的代码

public class MyVpnService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate(){
super.onCreate();

VpnService.Builder builder = new VpnService.Builder();
ParcelFileDescriptor localTunnel = builder
.addAddress("192.168.2.2", 24)
.addRoute("0.0.0.0", 0)
.addDnsServer("192.168.1.1")
.establish();

String[] appPackages = {
"com.android.chrome",
"com.google.android.youtube",
"com.example.a.missing.app"};

VpnService.Builder builder = new VpnService.Builder();
PackageManager packageManager = getPackageManager();
for (String appPackage: appPackages) {
try {
packageManager.getPackageInfo(appPackage, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.addAllowedApplication(appPackage);
}
} catch (PackageManager.NameNotFoundException e) {
// The app isn't installed.
}
}
localTunnel = builder
.addAddress("2001:db8::1", 64)
.addRoute("::", 0)
.establish();
}
}

list

<service android:name=".MyVpnService"
android:permission="android.permission.BIND_VPN_SERVICE">
<intent-filter>
<action android:name="android.net.VpnService"/>
</intent-filter>
<meta-data android:name="android.net.VpnService.SUPPORTS_ALWAYS_ON"
android:value="false"/>
</service>

下面有一条红线 VpnService.Builder 构建器 = new VpnService.Builder();

错误读取

android.net.VpnService 不是封闭类

最佳答案

这是因为 MyVpnService 扩展了 android.app.Service 而不是 android.net.VpnService,它必须这样做才能充当 VPN 服务。

关于java - android.net.VPNServicec 不是封闭类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59823864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com