如何知道何时没有活动就准备好Android VPN?

问题描述

当您想启动Vpn时,我们可以执行以下操作:

VpnService.prepare(activity)

然后:

override fun onActivityResult(requestCode: Int,resultCode: Int,data: Intent?) {
    super.onActivityResult(requestCode,resultCode,data)
    if (requestCode == REQUEST_VPN && resultCode == AppCompatActivity.RESULT_OK) {
 
        }

    }
}

但是我是在活动之外这样做的:

private void setupVPN() {
     if (vpnInterface == null) {

         jni_context = jni_init(Build.VERSION.SDK_INT);
        Builder builder = new Builder();
        builder.addAddress(VPN_ADDRESS,1);

        vpnInterface = getBuilder().establish();//.setSession(getString(R.string.app_name)).setConfigureIntent(pendingIntent).establish();

        if (vpnInterface != null) {
             startNative();
        } else {
            // vpn Failed to establish,prepare may not call or called but not yey prepared,// we will try 3 times or destroy vpn
            Timber.e("vpnInterface Failed to establish");
            vpnSetupAttempts++;
            if (vpnSetupAttempts < 4) {
                VpnService.prepare(this);
                final Handler handler = new Handler(Looper.getMainLooper());
                handler.postDelayed(this::setupVPN,2000);
            } else {
                vpnSetupAttempts = 0;
                logNonFatalException("vpnInterface Failed to establish");
                stopVpn();

            }
        }

    }
}

所以基本上我是在等待VPN的准备而没有任何指示, 这是可行的,但感觉不是一个好习惯,准备VPN后,还有其他方法可以监听吗?某种听众?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)