wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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 { }
        }
    }
}