尝试在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不是一个封闭的类
最佳答案
这是因为MyVpnService
扩展了android.app.Service
而不是android.net.VpnService
,它必须充当VPN服务。