码迷,mamicode.com
首页 > 移动开发 > 详细

Forms Android System Download

时间:2015-08-01 00:49:33      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
  1 不用自己写下载文件的功能,直接调用系统下载服务
  2  forms 接口
  3 
  4 using System;
  5 using System.Collections;
  6 using System.Collections.Generic;
  7 
  8 namespace
  9 {
 10     public interface ISystemDownloadFile
 11     {
 12         //下载文件
 13         void  DownloadThread (string strUrl,string str1);
 14     }
 15 }
 16 
 17 平台实现
 18 using System;
 19 using System.Collections;
 20 using System.Collections.Generic;
 21 using System.Threading.Tasks;
 22 using System.Threading;
 23 
 24 using Xamarin.Forms;
 25 
 26 using Android.Net;
 27 using Android.App;
 28 using Android.Content;
 29 using Android.Widget;
 30 using Android.Runtime;
 31 using Android.OS;
 32 using Android.Telephony;
 33 using Android.Util;
 34 using Java.Net;
 35 using Java.IO;
 36 using;
 37 using.TDroid;
 38 
 39 [assembly:Dependency(typeof(SystemDownloadFile))]
 40 namespace.TDroid
 41 {
 42     public class SystemDownloadFile : Java.Lang.Object,ISystemDownloadFile
 43     {
 44 
 45         private DownloadManager downloadManager;
 46         private long downloadId = 0;
 47         private string filename;
 48 
 49         public SystemDownloadFile()
 50         {
 51             initData ();
 52         }
 53             
 54         public void DownloadThread(string strUrl,string str1)
 55         {
 56             getFilename (strUrl);
 57 
 58             string strFolder = Android.OS.Environment.ExternalStorageDirectory + "/Download/";
 59             File folder = new File (strFolder);
 60             if (!folder.Exists () || !folder.IsDirectory) {
 61                 folder.Mkdirs ();
 62             }
 63             DownloadManager.Request request = new DownloadManager.Request (Android.Net.Uri.Parse (strUrl));
 64             request.SetDestinationInExternalPublicDir (strFolder, filename);
 65             request.SetTitle (filename);
 66             request.SetDescription (filename);
 67             request.SetNotificationVisibility (DownloadVisibility.VisibleNotifyCompleted);
 68             request.SetVisibleInDownloadsUi (false);
 69             downloadId = downloadManager.Enqueue (request);
 70         }
 71 
 72         /*
 73         public async Task SaveTextAsync (string filename, string text)
 74         {
 75             var docsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
 76             var path = Path.Combine(docsPath, filename);
 77 
 78             using (StreamWriter sw = File.CreateText(path))
 79             {
 80                 await sw.WriteAsync(text);
 81             }
 82         }
 83         */
 84 
 85         private void getFilename(string strUrl)
 86         {
 87             filename = strUrl.Substring (strUrl.LastIndexOf("/")+1);
 88 
 89         }
 90 
 91         private void initData()
 92         {
 93             downloadManager = (DownloadManager)((Activity)Forms.Context).GetSystemService(Android.Content.Context.DownloadService);
 94         }
 95         /*
 96         public void DownloadThread(string strUrl,string str1)
 97         {
 98             string strUrl1 = null;
 99             string str11 = null;
100             Thread thread = new Thread (delegate(){DownloadFile(strUrl,str1);});
101             strUrl1 = strUrl;
102             str11 = str1;
103             thread.Start ();
104         }*/
105 
106         /*
107         public void DownloadFile (string strUrl,string str1)
108         {
109             try {
110                 Java.Net.URL url = new Java.Net.URL (strUrl);
111                 connection = url.OpenConnection ();
112                 if (connection.ConnectTimeout == 5) {
113                     Log.Info ("---->", "网络有问题");
114                 }
115                 //inputStream = connection.InputStream;
116                 inputStream = (InputStream)connection.Content;
117             } catch (Exception ex) {
118                 //Log.Info ("---->", ex.Data);
119             }
120             string filename = strUrl.Substring (strUrl.LastIndexOf ("/") + 1);
121             string savePath = Android.OS.Environment.ExternalStorageDirectory + "/DownloadFile";
122             File file1 = new File (savePath);
123             if (!file1.Exists()) {
124                 file1.Mkdir ();
125             }
126             string savePathString =  Android.OS.Environment.ExternalStorageDirectory + "/DownloadFile/" + filename;
127             File file = new File (savePathString);
128             if (!file.Exists()) {
129                 try{
130                     file.CreateNewFile();
131                 }catch(IOEaxception e){
132                 }
133             }
134             try{
135                 outputStream = new FileOutputStream(file);
136                 byte[] buffer = new byte[1024*4];
137                 FileLength = connection.ContentLength;
138                 while(DownedFileLength < FileLength){
139                     outputStream.Write(buffer);
140                     DownedFileLength += inputStream.Read(buffer);
141                 }
142             }catch(FileNotFoundException e){
143             }catch(IOException e1){
144         }
145 
146         }*/
147     }        
148 }
View Code

 

Forms Android System Download

标签:

原文地址:http://www.cnblogs.com/techidea/p/4693386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!