Question

I create a vpnservice with this codes:

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            if (mThread != null)
            {
                mThread.Interrupt();
            }

            mThread = new Java.Lang.Thread(this,"360VpnThread");
            mThread.Start();

            return StartCommandResult.Sticky;
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }

        public void Run()
        {
            var builder = new VpnService.Builder(this);
            builder.SetSession(PackageName)
                .SetMtu(1460)
                .AddAddress("10.0.6.2", 24)
                .AddDnsServer("8.8.8.8").AddRoute("0.0.0.0", 8);

            mInterface = builder.Establish();

            if (mInterface == null)
            {
                StopSelf();
            }
        }

but i get this error:

java.lang.SecurityException: parspeed360.android.VpnService360 does not require android.permission.BIND_VPN_SERVICE

Already i add these to android manifest:

<application android:label="360.Android" android:icon="@drawable/Icon">
    <service android:name=".Parspeed360.Android.VpnService360"
             android:label="@string/ApplicationName"
             android:exported="false"
                android:permission="android.permission.BIND_VPN_SERVICE">
      <intent-filter>
        <action android:name="android.net.VpnService"/>
      </intent-filter>
    </service>
  </application>

please help me.

No correct solution

OTHER TIPS

Well, Arman Kabir was not so clear on HOW this problem is solved for him. After searching on Google Source Code, I figured out what was wrong for me.

If you write this code before you start your service:

Intent vpnServiceIntent = new Intent(Application.Context, typeof(LocalVpnService));

var resolveInfoList = PackageManager.QueryIntentServices(vpnServiceIntent, 0);

Application.Context.StartService(vpnServiceIntent);

And inspect resolveInfoList as follow:

resolveInfoList[0].ServiceInfo.Name // => "md55bfed5bb232464f797409dd275ac40fc.LocalVpnService"
resolveInfoList[0].ServiceInfo.Permission // => (null)

So I got this working when I change my service name in Manifest:

<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name">
    <service
      android:name="md55bfed5bb232464f797409dd275ac40fc.LocalVpnService"
      android:permission="android.permission.BIND_VPN_SERVICE">
      <intent-filter>
          <action android:name="android.net.VpnService" />
      </intent-filter>
  </service>
</application>

Maybe this is some Xamarin specificity...

My Service was declared as:

namespace kitkattest
{
    [Service]
    public class LocalVpnService : VpnService
    {
        // ...
    }
}

Just add the BIND_VPN_SERVICE permission to the service

namespace MyNamespace
{
    [Service(Label = "My Label", Permission = "android.permission.BIND_VPN_SERVICE")]
    public class MyService : VpnService
    {
        ...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top