JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
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
using System;
using Android.Content;
using Android.OS;
using Shared;
 
namespace SharedMethod
{
    public static class SharedMethod
    {
        public static bool IsBackground;
        //public static UIApplication sharedApp;
        public static void ChangeSkin ()
        {
            //if (UserConfig.Instance.SkinCode == 0) {
            //    sharedApp.StatusBarStyle = UIStatusBarStyle.LightContent;
            //    Shared.Application.Skin = UserConfig.Instance.CurrentSkinName;
            //} else if (UserConfig.Instance.SkinCode == 1) {
            //    sharedApp.StatusBarStyle = UIStatusBarStyle.Default;
            //    Shared.Application.Skin = UserConfig.Instance.CurrentSkinName;
            //}
        }
 
 
 
        /// <summary>        
        /// c#,.net 下载文件        
        /// </summary>        
        public static void DownloadFile (Action<string> action)
        {
            float percent = 0;
            try {
#if wallon
                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create ("http://intercom.hdlcontrol.com/AppDownload/apk/teston.apk");
#else
                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create ("http://intercom.hdlcontrol.com/AppDownload/apk/hdlon.apk");
#endif
                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse ();
                long totalBytes = myrp.ContentLength;
 
                System.IO.Stream st = myrp.GetResponseStream ();
                System.IO.Stream so = new System.IO.FileStream (new Java.IO.File (Android.OS.Environment.GetExternalStoragePublicDirectory (Android.OS.Environment.DirectoryDownloads), "on.apk").Path, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
                long totalDownloadedByte = 0;
                byte [] by = new byte [1024];
                int osize = st.Read (by, 0, (int)by.Length);
                while (osize > 0) {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    so.Write (by, 0, osize);
 
                    osize = st.Read (by, 0, (int)by.Length);
 
                    percent = (float)totalDownloadedByte / (float)totalBytes * 100;
                    System.Console.WriteLine (percent);
                    action (((int)percent).ToString ());
                }
                so.Close ();
                st.Close ();
 
                install (Shared.Application.Activity);
            } catch (System.Exception ex) {
                System.Console.WriteLine (ex.Message);
            }
        }
 
        static void install (Context context)
        {
            var file = new Java.IO.File (Android.OS.Environment.GetExternalStoragePublicDirectory (Android.OS.Environment.DirectoryDownloads), "on.apk");
 
            var intent = new Intent (Intent.ActionView);
            // 由于没有在Activity环境下启动Activity,设置下面的标签
            intent.SetFlags (ActivityFlags.NewTask);
 
            if (BuildVersionCodes.M < Android.OS.Build.VERSION.SdkInt) {
                //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致   参数3  共享的文件
                var apkUri = Android.Support.V4.Content.FileProvider.GetUriForFile (context, "com.hdl.in.fileProvider", file);
                //添加这一句表示对目标应用临时授权该Uri所代表的文件
                intent.AddFlags (ActivityFlags.GrantReadUriPermission);
                intent.SetDataAndType (apkUri, "application/vnd.android.package-archive");
            } else {
                intent.SetDataAndType (Android.Net.Uri.FromFile (file), "application/vnd.android.package-archive");
            }
            context.StartActivity (intent);
        }
 
        public static PageLayout CurPageLayout = null;
    }
}