using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
|
using Android.App;
|
using Android.Content;
|
using Android.OS;
|
using Android.Runtime;
|
using Android.Views;
|
using Android.Widget;
|
using Android.Net.Wifi;
|
using com.hdl.on;
|
|
namespace Shared.Other
|
{
|
[Service]
|
public class MusicService : Service
|
{
|
/// <summary>
|
/// 本地音乐的监听服务
|
/// </summary>
|
private Server server;
|
/// <summary>
|
/// 是否正在运行
|
/// </summary>
|
private bool isRunning = false;
|
public override IBinder OnBind (Intent intent)
|
{
|
return null;
|
}
|
public override void OnCreate ()
|
{
|
base.OnCreate ();
|
startServer ();
|
}
|
|
public override void OnDestroy ()
|
{
|
base.OnDestroy ();
|
stopServer ();
|
}
|
/// <summary>
|
/// WebServer 本地路径
|
/// </summary>
|
/// <value>The get document root.</value>
|
string documentRoot => Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/WebServer/";
|
/// <summary>
|
/// 初始化WebServer的路径
|
/// </summary>
|
void initDocumentRoot ()
|
{
|
try {
|
if (!(new Java.IO.File (documentRoot)).Exists ()) {
|
new Java.IO.File (documentRoot).Mkdir ();
|
var bout = new Java.IO.BufferedWriter (new Java.IO.FileWriter (documentRoot + "index.html"));
|
bout.Write ("<html><head><title>Android Webserver</title>");
|
bout.Write ("</head>");
|
bout.Write ("<body>Willkommen auf dem Android Webserver.");
|
bout.Write ("<br><br>Die HTML-Dateien liegen in " + documentRoot + ", der Sourcecode dieser App auf ");
|
bout.Write ("<a href=\"https://github.com/bodeme/androidwebserver\">Github</a>");
|
bout.Write ("</body></html>");
|
bout.Flush ();
|
bout.Close ();
|
bout = new Java.IO.BufferedWriter (new Java.IO.FileWriter (documentRoot + "403.html"));
|
bout.Write ("<html><head><title>Error 403</title>");
|
bout.Write ("</head>");
|
bout.Write ("<body>403 - Forbidden</body></html>");
|
bout.Flush ();
|
bout.Close ();
|
bout = new Java.IO.BufferedWriter (new Java.IO.FileWriter (documentRoot + "404.html"));
|
bout.Write ("<html><head><title>Error 404</title>");
|
bout.Write ("</head>");
|
bout.Write ("<body>404 - File not found</body></html>");
|
bout.Flush ();
|
bout.Close ();
|
}
|
} catch (Exception e) {
|
Android.Util.Log.Verbose ("ERROR", e.Message);
|
}
|
}
|
|
void startServer()
|
{
|
try {
|
if(isRunning){
|
return;
|
}
|
isRunning = true;
|
initDocumentRoot ();
|
server = new Server (documentRoot, ApplicationContext);
|
server.Start ();
|
}
|
catch (Exception e)
|
{
|
isRunning = false;
|
}
|
}
|
|
void stopServer ()
|
{
|
try {
|
if (null != server) {
|
server.stopServer ();
|
server.Interrupt ();
|
isRunning = false;
|
}
|
} catch { }
|
}
|
}
|
}
|