zoukankan      html  css  js  c++  java
  • Forms Android System Download

      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
  • 相关阅读:
    085 Maximal Rectangle 最大矩形
    084 Largest Rectangle in Histogram 柱状图中最大的矩形
    083 Remove Duplicates from Sorted List 有序链表中删除重复的结点
    082 Remove Duplicates from Sorted List II 有序的链表删除重复的结点 II
    081 Search in Rotated Sorted Array II 搜索旋转排序数组 ||
    080 Remove Duplicates from Sorted Array II 从排序阵列中删除重复 II
    079 Word Search 单词搜索
    078 Subsets 子集
    bzoj2326: [HNOI2011]数学作业
    bzoj2152: 聪聪可可
  • 原文地址:https://www.cnblogs.com/techidea/p/4693386.html
Copyright © 2011-2022 走看看