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
{
///
/// 本地音乐的监听服务
///
private Server server;
///
/// 是否正在运行
///
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 ();
}
///
/// WebServer 本地路径
///
/// The get document root.
string documentRoot => Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/WebServer/";
///
/// 初始化WebServer的路径
///
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 ("
Android Webserver");
bout.Write ("");
bout.Write ("Willkommen auf dem Android Webserver.");
bout.Write ("
Die HTML-Dateien liegen in " + documentRoot + ", der Sourcecode dieser App auf ");
bout.Write ("Github");
bout.Write ("");
bout.Flush ();
bout.Close ();
bout = new Java.IO.BufferedWriter (new Java.IO.FileWriter (documentRoot + "403.html"));
bout.Write ("Error 403");
bout.Write ("");
bout.Write ("403 - Forbidden");
bout.Flush ();
bout.Close ();
bout = new Java.IO.BufferedWriter (new Java.IO.FileWriter (documentRoot + "404.html"));
bout.Write ("Error 404");
bout.Write ("");
bout.Write ("404 - File not found");
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 { }
}
}
}