using Android.App; using Android.Widget; using Android.OS; using Shared; using Android.Content.PM; using System; using Android.Content; using Android.Net; using Shared.Phone.UserView; using CN.Jpush.Android.Api; using CN.Jpush.Android.Service; using Org.Json; using Microsoft.AppCenter; using Microsoft.AppCenter.Analytics; using Microsoft.AppCenter.Crashes; using Com.Tencent.MM.Sdk.Openapi; using ZXing.Mobile; using System.IO; namespace com.hdl.home { [Activity(Name = "com.hdl.home.wxapi.WXEntryActivity", Exported = true, Theme = "@android:style/Theme.Translucent", LaunchMode = Android.Content.PM.LaunchMode.SingleTask)] class WXEntryActivity : Android.App.Activity, IWXAPIEventHandler { public void OnReq(BaseReq p0) { } protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); Intent = intent; com.hdl.home.Application.api.HandleIntent(intent, this); //Finish(); } public static Action RespAction; public void OnResp(BaseResp p0) { if (p0 is SendAuth.Resp) { var resp = p0 as SendAuth.Resp; switch (resp.ErrCode) { case SendAuth.Resp.InnerErrCode.ErrOk: var result = new System.Net.WebClient { }.DownloadString($"https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx2ec8f53f6fa36e82&secret=a08585cd6ff2ce64570b9e7e6525dd8e&code={resp.Token}&grant_type=authorization_code"); RespAction?.Invoke(result); break; default: RespAction?.Invoke(null); break; //case SendAuth.Resp.InnerErrCode.ErrUserCancel: // break; //case SendAuth.Resp.InnerErrCode.ErrAuthDenied: // break; } } this.Finish(); } //IWXAPI api; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // 通过WXAPIFactory工厂,获取IWXAPI的实例 //api = WXAPIFactory.CreateWXAPI(this, "wx2ec8f53f6fa36e82", false); // 将应用的appId注册到微信 //api.RegisterApp("wx2ec8f53f6fa36e82"); com.hdl.home.Application.api.HandleIntent(Intent, this); } } /// 
 /// 设定为默认启动的Application
 /// 
 [Android.App.Application]
 public class Application : Android.App.Application
 {
 public Application(IntPtr handle, Android.Runtime.JniHandleOwnership ownerShip) : base(handle, ownerShip) { }

 public override void OnCreate()
 { //安卓配网初始 //var result = Com.Mediatek.Elian.ElianNative.LoadLib(); //if (!result) //{ // System.Console.WriteLine("Error,can't load elianjni lib"); //} initAll();
 base.OnCreate(); // 通过WXAPIFactory工厂,获取IWXAPI的实例 api = WXAPIFactory.CreateWXAPI(this, "wx2ec8f53f6fa36e82", true); // 将应用的appId注册到微信 api.RegisterApp("wx2ec8f53f6fa36e82"); } internal static IWXAPI api; public static void WXLogin() { var req = new SendAuth.Req { Scope = "snsapi_userinfo", State = "ZigbeeApp" }; api.SendReq(req); } /// 
 /// 所有初始化全部在这个方法实现
 /// 
 void initAll()
 { string checkFile = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Shared.Phone.UserCenter.DirNameResourse.OpenGbsFile); Shared.Application.IsGpsEnable = System.IO.File.Exists(checkFile); //设置极光调试模式,为false时只打印警告信息 // System.Console.WriteLine($"AAA : {System.DateTime.Now.ToString()}"); #if Release
 JPushInterface.SetDebugMode(false); //保存手机名称到本地文件 //System.Console.WriteLine($"BBB : {System .DateTime .Now .ToString ()}"); JPushInterface.Init(this); //System.Console.WriteLine($"CCC : {System.DateTime.Now.ToString()}"); #endif BaseActivity.BackKeyAction = () => { try { if (Shared.Common.CommonPage.BackKeyCanClick == false) { //不允许按返回键 return; } if (Shared.Common.CommonPage.Loading != null) { Console.WriteLine(Shared.Common.CommonPage.Loading.CurStatus); if (Shared.Common.CommonPage.Loading.CurStatus) { //Shared.SimpleControl.MainPage.Loading.Hide (); return; } } var lastView = Shared.Application.MainPage.GetChildren(Shared.Application.MainPage.ChildrenCount - 1); if (lastView != null && lastView.Tag?.ToString() == "Dialog") { Console.WriteLine("Remove Dialog!!!!"); var lastView2 = Shared.Application.MainPage.GetChildren(Shared.Application.MainPage.ChildrenCount - 2); lastView.RemoveFromParent(); lastView2.RemoveFromParent(); Console.WriteLine("Remove Dialog End!!!!"); return; } } catch (System.Exception ezx) { Console.WriteLine(ezx.Message); } if (HomePage.Instance.ChildrenCount > 1) { Console.WriteLine("Remove PageLayout Children!!!!"); HomePage.Instance.GetChildren(HomePage.Instance.ChildrenCount - 1).RemoveFromParent(); Console.WriteLine("Remove PageLayout Children End!!!!"); } else { BaseActivity.VerifyDateTime = DateTime.MaxValue; Console.WriteLine("BaseActivity VerifyDateTime Max!!!!"); } }; 
 BaseActivity.OnCreateActoin += (activity, application) => { Shared.Application.FontSize = 12; AppCenter.Start("4802834a-e7e9-4dd8-93f1-c2f88f0bd464", typeof(Analytics), typeof(Crashes));
#if Release
 //保存获取的极光服务器上的注册ID到本地文件
 var registrationId = JPushInterface.GetRegistrationID(activity); System.Console.WriteLine("registrationId-极光id=" + registrationId); if (!string.IsNullOrEmpty(registrationId))
 {
 Shared.Common.Config.Instance.RegistrationID = registrationId;
 Shared.Common.Config.Instance.Save();
 }
#endif
 };
 BaseActivity.RefreshUIAction += (activity) => {
 Shared.Language.CurrentLanguage = "Chinese";
 Shared.Common.CommonPage.Instance.Show();
 };
 BaseActivity.OnResumeAction += (activity) => {
 };
 BaseActivity.NetworkStateChanged += (v) =>
 {
 //网络状态变化处理事件 Shared.Common.CommonPage.nowNetworkMode = v;
 };
 BaseActivity.OnDestroyAction += (activity) => {
 //socket停止连接
 //ZigBee.Device.ZbGateway.FindGateWaySocket.Stop();
 };

 HDLUtils.SetAuthoritiesName("com.hdl.home.fileProvider");
 }

 public static bool IsShowTip = true;
 JpushNotificationReceiver myReceiver = new JpushNotificationReceiver { };

 /// 
 /// 项目启动时网络查询
 /// 
 void checkSomeInfo()
 {
 var status = isNetworkAvailable(this);
 string internetStatus = "Available";
 if (!status)
 {
 internetStatus = "UnaVailable";
 }
 else
 {
 var isWifi = isWifiConnected(this);
 if (isWifi)
 {
 internetStatus = "WiFiConnect";
 }
 else
 {
 internetStatus = "MobileConnect";
 }
 }

 int connectState = 0;
 if (internetStatus == "UnaVailable")
 {
 connectState = 0;
 }
 else if (internetStatus == "WiFiConnect")
 {
 connectState = 2;
 }
 else
 {
 connectState = 1;
 }
 //ZigBee.Device.ZbGateway.CheckConnection(connectState);
 }

 /// 
 /// 网络是否可用
 /// 
 /// true, if network available was ised, false otherwise.
 /// Context.
 bool isNetworkAvailable(Context context)
 {
 var cm = (ConnectivityManager)context.GetSystemService(ConnectivityService);

 if (cm == null || cm.ActiveNetworkInfo == null)
 {
 //当前网络不可用
 return false;
 }
 else
 {
 return cm.ActiveNetworkInfo.IsAvailable;
 }
 }

 /// 
 /// 网络是否连接
 /// 
 /// true, if wifi connected was ised, false otherwise.
 /// Context.
 bool isWifiConnected(Context context)
 {
 if (context != null)
 {
 var cm = (ConnectivityManager)context.GetSystemService(ConnectivityService);

 var mWiFiNetworkInfo = cm.GetNetworkInfo(ConnectivityType.Wifi);
 if (Android.Net.NetworkInfo.State.Connected == mWiFiNetworkInfo.GetState())
 {
 //当前网络是Wi-Fi连接
 return true;
 }

 var moWiFiNetworkInfo = cm.GetNetworkInfo(ConnectivityType.Mobile);
 if (Android.Net.NetworkInfo.State.Connected == moWiFiNetworkInfo.GetState())
 {
 //当前网络是Mobile连接
 return false;
 }
 }
 return false;
 } } public class JpushNotificationReceiver : BroadcastReceiver { private static string TAG = "JpushNotificationReceiver"; private NotificationManager nm; static string ACTION = "android.intent.action.BOOT_COMPLETED"; public override void OnReceive(Context context, Intent intent) { if (nm == null) { nm = (NotificationManager)context.GetSystemService(Context.NotificationService); } //base.OnReceive (context, intent); try { Bundle bundle = intent.Extras; if (intent.Action == ACTION) { //开机自动服务自动启动,PushService是要启动的服务 Intent service = new Intent(context, typeof(PushService)); context.StartService(service); } //接收Registration Id if (JPushInterface.ActionRegistrationId == intent.Action) { string regId = bundle.GetString(JPushInterface.ExtraRegistrationId); if (!string.IsNullOrEmpty(regId)) { Shared.Common.Config.Instance.RegistrationID = regId; Shared.Common.Config.Instance.Save(); } } //用户点击打开了通知 else if (JPushInterface.ActionNotificationOpened == intent.Action) { OpenNotification(context, bundle); } //接受到推送下来的通知 else if (JPushInterface.ActionNotificationReceived == intent.Action) { //int notifactionId = bundle.GetInt(JPushInterface.ExtraNotificationId); ReceivingNotification(context, bundle); //Shared.Common.CommonPage.Instance.SingOut(); } //接收到推送下来的自定义消息 else if (JPushInterface.ActionMessageReceived == intent.Action) { bundle.GetString(JPushInterface.ExtraMessage); } else { } } catch (System.Exception e) { System.Console.WriteLine("极光推送出错:" + e.Message); } } /// /// 接收消息通知 /// /// Context. /// Bundle. private void ReceivingNotification(Context context, Bundle bundle) { String title = bundle.GetString(JPushInterface.ExtraNotificationTitle); System.Console.WriteLine(TAG, " title : " + title); String message = bundle.GetString(JPushInterface.ExtraAlert); System.Console.WriteLine(TAG, "message : " + message); String extras = bundle.GetString(JPushInterface.ExtraExtra); System.Console.WriteLine(TAG, "extras : " + extras); //处理极光消息推送的逻辑函数 Shared.Phone.UserCenter.HdlJiguangMsgPushLogic.Current.AdjustJiguangMsgPush(title, message, extras); } /// /// 打开消息显示界面 /// /// Context. /// Bundle. private void OpenNotification(Context context, Bundle bundle) { String extras = bundle.GetString(JPushInterface.ExtraExtra); String myValue = ""; try { JSONObject extrasJson = new JSONObject(extras); myValue = extrasJson.OptString("myKey"); } catch (Exception e) { System.Console.WriteLine(TAG, "Unexpected: extras is not a valid json", e); return; } Intent i = new Intent(context, typeof(BaseActivity));//Intent intent=new Intent( 起始组件对象 , 目标 Service.class); i.PutExtras(bundle); i.SetFlags(ActivityFlags.NewTask); context.StartActivity(i); //if (TYPE_THIS.equals(myValue)) //{ //Intent mIntent = new Intent(context, ThisActivity.class); //mIntent.putExtras(bundle); //mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //context.startActivity(mIntent); //} //else if (TYPE_ANOTHER.equals(myValue)) //{ //Intent mIntent = new Intent(context, AnotherActivity.class); //mIntent.putExtras(bundle); //mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //context.startActivity(mIntent); //} } } } namespace Shared { public static class QRCode { static ZXing.Mobile.MobileBarcodeScanner scanner; public static void ScanQRcode(Action action, string cancel = "取消", string flashText = "闪光灯", string titleText = "二维码扫描") { ((BaseActivity)Shared.Application.Activity).SetCamera(async (obj) => { if (obj) { if (scanner == null) { MobileBarcodeScanner.Initialize(Shared.Application.Activity.Application); var mZXingCustomScanView = new GateWay.Droid.ZXingCustomScanView(Shared.Application.Activity.Application); mZXingCustomScanView.cancelTextView.Text = cancel; mZXingCustomScanView.flashTextView.Text = flashText; mZXingCustomScanView.titleTextView.Text = titleText; var bOn = false; scanner = new ZXing.Mobile.MobileBarcodeScanner() { UseCustomOverlay = true, CustomOverlay = mZXingCustomScanView }; mZXingCustomScanView.OnCancel += () => { scanner?.Cancel(); }; mZXingCustomScanView.OnTorch += () => { bOn = !bOn; scanner?.Torch(bOn); }; } var result = await scanner.Scan(); if (result != null) action?.Invoke(result.Text); else action?.Invoke(null); } else { action?.Invoke(null); } }); } public static byte[] BytesFromText(string text, int width = 300, int height = 300) { var barcodeWriter = new ZXing.Mobile.BarcodeWriter { Format = ZXing.BarcodeFormat.QR_CODE, Options = new ZXing.Common.EncodingOptions { Width = width, Height = height, Margin = 0 } }; barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer(); var bitmap = barcodeWriter.Write(text); using (var stream = new MemoryStream()) { bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, stream); // this is the diff between iOS and Android stream.Position = 0; return stream.ToArray(); } } } }