Я пытаюсь остановить службу переднего плана, но кажется, что мой код не работает должным образом
Intent intent = new Intent(SplashScreenActivity.this, DNSService.class);
stopService(intent);
И это мой источник службы:
public class DNSService extends VpnService {
public final static String DNS_MODEL = "DNSModelIntent";
private VpnService.Builder builder = new VpnService.Builder();
private ParcelFileDescriptor fileDescriptor;
private Thread mThread;
private boolean shouldRun = true;
private DatagramChannel tunnel;
private DNSModel dnsModel;
public static final String ACTION_CONNECT = DNSService.class.getName() + ".START";
public static final String ACTION_DISCONNECT = DNSService.class.getName() + ".STOP";
@Override
public void onDestroy() {
stopSelf();
stopForeground(true);
super.onDestroy();
}
@Override
public void onCreate() {
super.onCreate();
}
private void setTunnel(DatagramChannel tunnel) {
this.tunnel = tunnel;
}
private void setFileDescriptor(ParcelFileDescriptor fileDescriptor) {
this.fileDescriptor = fileDescriptor;
}
@Override
public int onStartCommand(final Intent intent, int p1, int p2) {
mThread = new Thread(() -> {
try {
dnsModel = intent.getParcelableExtra(DNS_MODEL);
setFileDescriptor(builder.setSession(DNSService.this.getText(R.string.app_name).toString()).
addAddress("192.168.0.1", 24).addDnsServer(dnsModel.getFirstDns()).addDnsServer(dnsModel.getSecondDns()).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;
}
}