wp wp8: background transfer service

1. TransferPreferences property setting:

the maximum file size uploaded by the background transfer service is 5M

, and the maximum download size through the mobile network connection is 20M. If it exceeds this size, the property will be automatically changed to AllowBattery

without external power supply. The maximum download size is 100M, if it exceeds this size size, attribute is set to None

2. The maximum number of outstanding requests in each application queue is 25, the maximum number of concurrent requests is 2, and the number of transmissions for large queues is 500

3. The network connection array 3G network is lower than 50kbps, wifi /usb network below 100kbps will pause the transfer and retry





the above code:

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls ;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using wp8BackgroundTransfer.Resources;
using Microsoft.Phone.BackgroundTransfer;

namespace wp8BackgroundTransfer
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();

          
        }

        BackgroundTransferRequest transferRequest;

        private void download_click(object sender, System.Windows.RoutedEventArgs e)
        {

            if (BackgroundTransferService.Requests.Count() > 25)
            {
                return;
            }

            String transferFileName = "http://m.news.cn/version_wp8.txt";
            Uri transferUri;
            try
            {
                transferUri = new Uri(Uri.EscapeUriString(transferFileName), UriKind.RelativeOrAbsolute);
            }
            catch
            {
                return;
            }

            transferRequest = new BackgroundTransferRequest(transferUri);

            transferRequest.TransferPreferences = TransferPreferences.None;

            transferRequest.TransferProgressChanged += new EventHandler<BackgroundTransferEventArgs>(transfer_TransferProgressChanged);

            transferRequest.TransferStatusChanged += new EventHandler<BackgroundTransferEventArgs>(transfer_TransferStatusChanged);

            transferRequest.Method = "GET";

            string downloadFileName = transferFileName.Substring(transferFileName.LastIndexOf("/")+1);

            Uri downloadUri = new Uri("shared/transfers/"+ downloadFileName, UriKind.RelativeOrAbsolute);

            transferRequest.DownloadLocation = downloadUri;

            transferRequest.Tag = downloadFileName;

            try
            {
                BackgroundTransferService.Add(transferRequest);
            }
            catch
            {
                return;
            }

        }

        private void cancel_click(object sender, System.Windows.RoutedEventArgs e)
        {

            if(transferRequest != null)
                removeRequest(transferRequest.RequestId);

        }

        void transfer_TransferProgressChanged(object sender, BackgroundTransferEventArgs e)
        {
           
        }

        void transfer_TransferStatusChanged(object sender, BackgroundTransferEventArgs e)
        {
            BackgroundTransferRequest transfer = e.Request;

            switch (transfer.TransferStatus)
            {
                case TransferStatus.Completed:
                    {
                        if (transfer.StatusCode == 200 || transfer.StatusCode == 206)
                        {
                            removeRequest(transfer.RequestId);
                        }
                        else
                        {
                            removeRequest(transfer.RequestId);
                            if (transfer.TransferError != null)
                            {
                                System.Diagnostics.Debug.WriteLine("失败:"+transfer.TransferError);
                            }
                        }
                    }
                    break;
                case TransferStatus.WaitingForExternalPower:
                    {
                        System.Diagnostics.Debug.WriteLine("Insufficient power, cannot start");
                    }
                    break;
                case TransferStatus.WaitingForExternalPowerDueToBatterySaverMode:
                    {
                        System.Diagnostics.Debug.WriteLine("Power saving mode, cannot start");
                    }
                    break;
                case TransferStatus. WaitingForNonVoiceBlockingNetwork:
                    {
                        System.Diagnostics.Debug.WriteLine("Waiting for network");
                    }
                    break;
                case TransferStatus.WaitingForWiFi:
                    {
                        System.Diagnostics.Debug.WriteLine("等待wifi网络");
                    }
                    break;
            }
        }

        void removeRequest(string id)
        {
            BackgroundTransferRequest request = BackgroundTransferService.Find(id);
            if (request != null)
            {
                BackgroundTransferService.Remove(request);
            }
        }
       
    }
}

MainPage.xaml

<phone:PhoneApplicationPage
    x:Class="wp8BackgroundTransfer.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid that contains all page content -->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>


        <StackPanel Orientation="Vertical">


            <Button Content="download" Width="300" Height="100" Click="download_click"></Button>
            <Button Content="cancel" Width="300" Height="100" Click="cancel_click"></Button>
           
        </StackPanel>


    </Grid>

</phone:PhoneApplicationPage>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326373324&siteId=291194637
WP2