How can I monitor vpn tun close event?

259 Views Asked by At

About android vpn service . When I start my vpn app and start vpn, then start another vpn app and start vpn, my app's tun network interface is closed, bug my app's vpnservice is runing normally, why?

I want to know is there any method can let me know:my vpn's fd should close. Two may sulution:

  1. monitor network change event, can found that tun is closed.
  2. when another vpn established, my vpnservice received onDestroy callback . ---acturally onDestroy is not called.
2

There are 2 best solutions below

0
user7720271 On BEST ANSWER

I have found the reason:

    override fun onBind(intent: Intent): IBinder {
        return Binder()
    }

Nomally onBind should be :

public IBinder onBind(Intent intent) {
    if (intent != null && SERVICE_INTERFACE.equals(intent.getAction())) {
        return new Callback();
    }
    return null;
}

How can I found that? When another vpn app called prepare, the vpn service should stop,but I find a logcat: oneway function results for code 16777215 on binder at 0xb40000730f64d060 will be dropped but finished with status UNKNOWN_TRANSACTION It seems that the binder has error, so I go to check the onBind implmentation.

3
kamyar haqqani On

vpnservice onDestroy isn't supposed to be called when tun is closed. vpnservice's onRevoke will be called if another app creates a vpn tun for device.

@Override
public void onRevoke() {
    super.onRevoke();
    //here is the event
}