using System; using System.Net; using System.Threading.Tasks; using Android; using Android.App; using Android.Content; using Android.Net; 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 { /// /// 设定为默认启动的Application /// [Android.App.Application(Theme = "@style/MyTheme")] public class Application : Android.App.Application, Android.App.Application.IActivityLifecycleCallbacks { 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(); 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(); //后台回到前台 new HDL_ON.UI.AppUnlockPage().LoadPage(); } } public void OnActivityStopped(Activity activity) { mCount--; if (mCount == 0) { Console.WriteLine("HHH OnActivityStopped: background"); BusSocketStop(); //进入后台 UserInfo.Current.unlockTime = DateTime.Now; } } public override void OnTerminate() { base.OnTerminate(); UnregisterActivityLifecycleCallbacks(this); } public int mCount; ///// ///// 是否进入后台 ///// //public bool IsEnterBackground = false; /// /// 进入后台的时间 /// DateTime CloseTime = DateTime.Now; /// /// BusSocketStop /// void BusSocketStop() { //IsEnterBackground = true; CloseTime = DateTime.Now; MainPage.IsEnterBackground = true; //进入后台mqtt正在连接重置状态 HDL_ON.DAL.Mqtt.MqttClient.RemoteMqttIsConnecting = false; HDL_ON.DriverLayer.UdpSocket._BusSocket.Stop(); } /// /// BusSocketStart /// 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); } /// /// 查询通知权限,并提示 /// public void QueryNotificationPermission() { } } }