C# StartService意图中的Xamarin Android错误

C# StartService意图中的Xamarin Android错误,c#,android,android-intent,xamarin,vpn,C#,Android,Android Intent,Xamarin,Vpn,我使用以下代码创建VPN服务: public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { if (mThread != null) { mThread.Interrupt(); } mThread =

我使用以下代码创建VPN服务:

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();
            }
        }
但我得到了这个错误:

java.lang.SecurityException:parspeed360.android.VpnService360 不需要android.permission.BIND\u VPN\u服务

我已经将这些添加到android清单中:

<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>


请帮帮我。

好吧,阿曼·卡比尔不太清楚这个问题是如何为他解决的。在搜索之后,我发现了我的问题所在

如果在启动服务之前编写此代码:

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

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

Application.Context.StartService(vpnServiceIntent);
并检查resolveInfoList,如下所示:

resolveInfoList[0].ServiceInfo.Name // => "md55bfed5bb232464f797409dd275ac40fc.LocalVpnService"
resolveInfoList[0].ServiceInfo.Permission // => (null)
所以当我在清单中更改我的服务名称时,我实现了这一点:

<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>

只需将BIND_VPN_服务权限添加到服务中即可

namespace MyNamespace
{
    [Service(Label = "My Label", Permission = "android.permission.BIND_VPN_SERVICE")]
    public class MyService : VpnService
    {
        ...
    }
}

但我这样做并得到异常java.lang.securityexception:VpnService360在调用builder.build()时不需要android.premission.Bind_VPN_服务;这个问题为我解决了!
namespace MyNamespace
{
    [Service(Label = "My Label", Permission = "android.permission.BIND_VPN_SERVICE")]
    public class MyService : VpnService
    {
        ...
    }
}