zoukankan      html  css  js  c++  java
  • Windows Phone 官方示例学习:Background Transfer Service Sample(后台传输)

    此示例显示了如何使用后台传输服务在后台下载文件,即使当应用程序不再运行在前台。

    通过BackgroundTransferRequest 实现上传下载

    // Create the new transfer request, passing in the URI of the file to 
    // be transferred.
    BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri);
    
    // Set the transfer method. GET and POST are supported.
    transferRequest.Method = "GET";


    通过TransferPreferences设置,是否用Wifi下载,是否充电时下载

     // If the WiFi-only checkbox is not checked, then set the TransferPreferences
                // to allow transfers over a cellular connection.
                if (wifiOnlyCheckbox.IsChecked == false)
                {
                    transferRequest.TransferPreferences = TransferPreferences.AllowCellular;
                }
                if (externalPowerOnlyCheckbox.IsChecked == false)
                {
                    transferRequest.TransferPreferences = TransferPreferences.AllowBattery;
                }
                if (wifiOnlyCheckbox.IsChecked == false && externalPowerOnlyCheckbox.IsChecked == false)
                {
                    transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
                }

    使用独立存储保持文件

    //在独立存储中,判断是否文件已存在, 存在则删除
    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        string filename = downloadFile;
                        if (isoStore.FileExists(filename))
                        {
                            isoStore.DeleteFile(filename);
                        } 
                    }

    类BackgroundTransferRequest 属性:

    TransferStatus 传输状态

    TotalBytesToReceive 文件大小

    BytesReceived 已收到的文件大小

    代码下载:

    http://code.msdn.microsoft.com/wpapps/Background-Transfer-ce07e86f

  • 相关阅读:
    合并两个排序的链表
    反转链表
    java网络编程之TCP通讯
    java网络编程之UDP通讯
    Java中的线程同步机制
    阿里研发工程师面试题三个小结
    Android开发的进阶之路
    获取一个字符串中每一个字母出现的次数使用map集合
    Android常见面试题目
    Java垃圾回收
  • 原文地址:https://www.cnblogs.com/star250/p/3013900.html
Copyright © 2011-2022 走看看