【问题标题】:Xamarin Android Error in StartService IntentStartService Intent 中的 Xamarin Android 错误
【发布时间】:2014-03-21 16:51:38
【问题描述】:

我使用以下代码创建了一个 vpnservice:

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_VPN_SERVICE

我已经将这些添加到 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>

请帮帮我。

【问题讨论】:

  • 但我这样做并得到异常 java.lang.securityexception : VpnService360 在调用 builder.Establish() 时不需要 android.premission.Bind_VPN_Service;
  • 这个问题为我解决了!

标签: c# android android-intent xamarin vpn


【解决方案1】:

嗯,Arman Kabir 并不清楚如何为他解决这个问题。在搜索Google Source Code 后,我发现我出了什么问题。

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

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)

所以当我在 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>

也许这是 Xamarin 的一些特殊性...

我的服务被声明为:

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

【讨论】:

    【解决方案2】:

    只需将 BIND_VPN_SERVICE 权限添加到服务中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多