我正在尝试在我的应用中实施“以编程方式更改DNS逻辑”。一切运行正常,但是当我尝试通过Google Play下载/更新应用程序时,它不会开始就没有停止。当我停止服务时,一切正常,并且可以毫无问题地下载/更新应用程序。
这是我的服务:
public class VPNService extends VpnService {
private VpnService.Builder builder = new VpnService.Builder();
private ParcelFileDescriptor fileDescriptor;
private Thread mThread;
private boolean shouldRun = true;
private DatagramChannel tunnel;
public static final String ACTION_CONNECT = VPNService.class.getName() + ".START";
public static final String ACTION_DISCONNECT = VPNService.class.getName() + ".STOP";
@Override
public void onDestroy() {
if (mThread != null) {
mThread.interrupt();
}
super.onDestroy();
}
@Override
public void onCreate() {
super.onCreate();
startForeground(999, new Notification());
}
private void setTunnel(DatagramChannel tunnel) {
this.tunnel = tunnel;
}
private void setFileDescriptor(ParcelFileDescriptor fileDescriptor) {
this.fileDescriptor = fileDescriptor;
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
if (intent != null && ACTION_DISCONNECT.equals(intent.getAction())) {
onDestroy();
return Service.START_NOT_STICKY;
}
mThread = new Thread(() -> {
try {
setFileDescriptor(builder.setSession(VPNService.this.getText(R.string.app_name).toString()).
addAddress("192.168.0.1", 24).addDnsServer("94.155.240.3").addDnsServer("212.50.87.211").establish());
setTunnel(DatagramChannel.open());
tunnel.connect(new InetSocketAddress("127.0.0.1", 8087));
protect(tunnel.socket());
while (shouldRun)
Thread.sleep(100L);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
if (fileDescriptor != null) {
try {
fileDescriptor.close();
setFileDescriptor(null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
mThread.start();
return Service.START_STICKY;
}
public static void startVService(Context context) {
VPNService.prepare(context);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
ContextCompat.startForegroundService(context,new Intent(context, VPNService.class).setAction(ACTION_CONNECT));
else
context.startService(new Intent(context, VPNService.class).setAction(ACTION_CONNECT));
}catch (RuntimeException e){
Log.d("VPNService","Not allowed to start service Intent",e);
}
}
}
您可能必须从vpn中排除Playstore。
https://www.reddit.com/r/ProtonVPN/comments/bzh6ir/apps_not_downloadingupdating_via_google_play/
vpnBuilder.addDisallowedApplication(packageName); //套件名称为com.android.vending(Play商店)