[android.net.VPNServicec不是封闭的类

问题描述 投票:1回答:1

尝试在android Docs之后实现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();
    }
}

清单

<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 builder =新的VpnService.Builder();

错误读取

android.net.VpnService不是封闭的类

java android xml service vpn
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.