4

これは奇妙なものです。

Windows Phone 8 アプリのバックグラウンド エージェントは、デバッグ ビルドを実行している 1 日を通して完全に更新されているようです。ただし、リリース ビルドに変更すると、まったく実行されないか、ほとんど実行されません。これは天気アプリ用です。私のライブ タイルのコンテンツは 1 時間ごとに変わるはずです。時々 1 時間ごとに更新されるのを見たことがありますが、その後数時間停止し、突然再開します。アプリのバックグラウンド エージェントが OS によってブロックされることはありません。これは、バックグラウンド エージェントに問題がないか、ほとんど実行されていないか、まったく実行されていないことを示唆しています。

現時点では、Windows Phone 8.1 を実行している Lumia 1020 でアプリのデバッグ ビルドとリリース ビルドを使用しています。コード的には同じです。デバッグ ビルドは 30 分ごとに更新されます (ライブ タイルのタイムスタンプの原因はわかっています) が、リリース ビルドは 90 分前に作成されてから 1 回も更新されていません (ただし、そのバックグラウンド エージェントはまだ「許可されている」としてリストされています)。バッテリー セーバー アプリで実行します)。

エージェントを作成するメソッドは次のとおりです。

    private void AddAgent(string periodicTaskName)
    {
        periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;
        if (periodicTask != null)
        {
            RemoveAgent(periodicTaskName);
        }

        periodicTask = new PeriodicTask(periodicTaskName);
        periodicTask.Description = "LiveTileHelperUpdateTask";

        try
        {
            ScheduledActionService.Add(periodicTask);

            // If debugging is enabled, use LaunchForTest to launch the agent in 5 seconds.
            //#if(DEBUG_AGENT)
               ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(1));
            //#endif
        }
        catch (InvalidOperationException)
        {

        }
        catch (SchedulerServiceException)
        {

        }
    }

スケジュールされたエージェント コードは次のとおりです。

public class ScheduledAgent : ScheduledTaskAgent
{
    /// <remarks>
    /// ScheduledAgent constructor, initializes the UnhandledException handler
    /// </remarks>
    static ScheduledAgent()
    {
        // Subscribe to the managed exception handler
        Deployment.Current.Dispatcher.BeginInvoke(delegate
        {
            Application.Current.UnhandledException += UnhandledException;
        });
    }

    /// Code to execute on Unhandled Exceptions
    private static void UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        if (Debugger.IsAttached)
        {
            // An unhandled exception has occurred; break into the debugger
            Debugger.Break();
        }
    }

    /// <summary>
    /// Agent that runs a scheduled task
    /// </summary>
    /// <param name="task">
    /// The invoked task
    /// </param>
    /// <remarks>
    /// This method is called when a periodic or resource intensive task is invoked
    /// </remarks>
    protected async override void OnInvoke(ScheduledTask task)
    {
        // If the app has not been purchased and trial has expired,
        // don't do anything here.
        if( (bool) IsolatedStorageSettings.ApplicationSettings["TrialExpired"] == true )
            return;

        // Setup view model to get data from.    
        LiveTileViewModel viewModel = new LiveTileViewModel();
        await viewModel.getWeatherForTileLocation();

        // Use a dispatcher because we are NOT on the UI thread!
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            try
            {                
                RadFlipTileData extendedData = new RadFlipTileData();
                extendedData.IsTransparencySupported = true;

                // Determine which sized tile will be the live tile.
                // Only create a live tile for it due to memory constraints.
                string liveTileSize = IsolatedStorageSettings.ApplicationSettings["LiveTileSize"].ToString();
                switch (liveTileSize)
                {
                    case "SMALL TILE":
                        extendedData.SmallVisualElement = new LiveTileSmall()
                        {
                            Icon = viewModel.Location.Hourly.Data[0].Icon,
                            Temperature = viewModel.Location.Hourly.Data[0].Temperature
                        };
                    break;
                    case "REGULAR TILE":
                        // Code here similar to small tile's
                    break;
                    default:
                        // Code here similar to small tile's
                    break;
                }

                FreeMemory();

                foreach (ShellTile tile in ShellTile.ActiveTiles)
                {
                    LiveTileHelper.UpdateTile(tile, extendedData);
                    break;
                }

                NotifyComplete();
            }
            catch (Exception e)
            {
            }
        }); 
    }
}
}

バックグラウンド エージェントがデバッグ ビルドでのみ動作するという同様の経験がありましたか?

御時間ありがとうございます。

4

1 に答える 1

0

バックグラウンド タスクの実行を確認するために、デバッグ ビルドをアンインストールしてリリースをインストールしようとしましたか?

また、リリース ビルドのScheduledActionService.LaunchForTestメソッド呼び出しを削除してみてください。ドキュメントの注意セクションを参照してください。現在のコードでは、1 秒ごとにバックグラウンド タスクのテスト実行をトリガーするように指定しました。制限があり、この時間が 60 秒未満の場合、タスクが実行されない場合があります。

于 2015-01-16T23:48:17.627 に答える