wxr
2023-06-06 592974441a4df95fffd9167c90192da1a390b1c2
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Net;
using System.Threading.Tasks;
using Android;
using Android.App;
using Android.Content;
using Android.Net;
using Android.Net.Wifi;
using Android.OS;
using Android.Runtime;
using Android.Widget;
using CN.Jpush.Android.Api;
using HDL_ON;
using Java.Util;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Shared;
 
namespace HDL_ON_Android
{
    /// <summary>
    /// 设定为默认启动的Application
    /// </summary>
    [Android.App.Application(Theme = "@style/MyTheme")]
    public class Application : Android.App.Application, Android.App.Application.IActivityLifecycleCallbacks
    {
        //处理安卓接收不到udp命令问题
        WifiManager.MulticastLock multicast;
 
        static void reStartApp(Android.App.Application application)
        {
            var intent = new Intent(application, typeof(BaseActivity));
            intent.AddFlags(ActivityFlags.NewTask);
            application.StartActivity(intent);
            Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
        }
 
        public Application(IntPtr handle, Android.Runtime.JniHandleOwnership ownerShip) : base(handle, ownerShip) { }
        public override void OnCreate()
        {
 
            if (Shared.Application.Activity != null)
            {
                //reStartApp(this);
                return;
            }
 
            base.OnCreate();
 
            WifiManager manager = (WifiManager)this.GetSystemService(Context.WifiService);
 
            multicast = manager.CreateMulticastLock("UDPwifi");
 
 
            Language.CurrentLanguage = "Chinese";
            Locale locale1 = Locale.Default;
            var localeList = Resources.Configuration.Locale;
            Console.WriteLine("当前语言:" + localeList.Language);
            if (localeList.Language == "zh")
            {
                Language.CurrentLanguage = "Chinese";
            }
            else
            {
                Language.CurrentLanguage = "English";
            }
 
            RegisterActivityLifecycleCallbacks(this);
        }
 
        public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
        {
 
        }
 
        public void OnActivityDestroyed(Activity activity)
        {
            //throw new NotImplementedException ();
        }
 
        public void OnActivityPaused(Activity activity)
        {
            //throw new NotImplementedException ();
        }
 
        public void OnActivityResumed(Activity activity)
        {
            //throw new NotImplementedException ();
        }
 
        public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
        {
            //throw new NotImplementedException ();
        }
 
        public void OnActivityStarted(Activity activity)
        {
            //throw new NotImplementedException ();
            if (HDL_ON.OnAppConfig.Instance.FirstRunApp)
            {
                return;
            }
            mCount++;
            if (mCount == 1)
            {
                Console.WriteLine("HHH OnActivityStarted:foreground");
                BusSocketStart();
                multicast.Acquire();
 
                //后台回到前台
                new HDL_ON.UI.AppUnlockPage().LoadPage();
            }
 
        }
 
        public void OnActivityStopped(Activity activity)
        {
 
            mCount--;
            if (mCount == 0)
            {
                Console.WriteLine("HHH OnActivityStopped: background");
                BusSocketStop();
                multicast.Release();
                //进入后台
                UserInfo.Current.unlockTime = DateTime.Now;
 
            }
        }
 
 
        public override void OnTerminate()
        {
            base.OnTerminate();
            UnregisterActivityLifecycleCallbacks(this);
        }
 
        public int mCount;
 
        ///// <summary>
        ///// 是否进入后台
        ///// </summary>
        //public bool IsEnterBackground = false;
 
        /// <summary>
        /// 进入后台的时间
        /// </summary>
        DateTime CloseTime = DateTime.Now;
 
        /// <summary>
        /// BusSocketStop
        /// </summary>
        void BusSocketStop()
        {
            //IsEnterBackground = true;
            CloseTime = DateTime.Now;
            MainPage.IsEnterBackground = true;
            //进入后台mqtt正在连接重置状态
            HDL_ON.DAL.Mqtt.MqttClient.RemoteMqttIsConnecting = false;
            HDL_ON.DriverLayer.UdpSocket._BusSocket.Stop();
 
        }
 
        /// <summary>
        /// BusSocketStart
        /// </summary>
        void BusSocketStart()
        {
            //IsEnterBackground = false;
 
            if (CloseTime.AddSeconds(10) < DateTime.Now)
            {
                //后台超过10s就断开MQTT重连
                //if (HDL_ON.DriverLayer.Control.Ins.IsRemote)
                {
                    HDL_ON.DAL.Mqtt.MqttClient.DisConnectRemote("closeTime", false);
                }
            }
 
            MainPage.IsEnterBackground = false;
            HDL_ON.DriverLayer.UdpSocket._BusSocket.Start();
            HDL_ON.DriverLayer.Control.Ins.SearchLoaclGateway();
        }
 
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
 
        /// <summary>
        /// 查询通知权限,并提示
        /// </summary>
        public void QueryNotificationPermission()
        {
 
 
        }
 
 
 
    }
 
}