初学者,想用MAUI开发安卓程序,模拟器运行时遇到一个问题,百度了很多资料还是没解决,有说是权限问题,有说是进程问题,有说是模拟器问题的,但是都试了没解决,希望大拿看到能不吝赐教。
程序是在预置的程序里面,按钮的事件加了一个TCP监听,运行到_listener.Start()_时,报错System.Net.Sockets.SocketException: 'Permission denied',发布后的APK真机装上也出现同样的错误。
以下是AndroidManifest.xml配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
</manifest>
以下是MainPage.xaml.cs
using System.Net;
using System.Net.Sockets;
namespace MauiApp5;
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
}
private void OnCounterClicked(object sender, EventArgs e)
{
NetworkAccess accessType = Connectivity.Current.NetworkAccess;
if (accessType == NetworkAccess.Internet)
{
// Connection to internet is available
DisplayAlert("ces", "123", "OK");
TcpListener listener = new TcpListener(IPAddress.Any, 502);
**listener.Start();**
}
count++;
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
SemanticScreenReader.Announce(CounterBtn.Text);
}
}