コード例 #1
0
ファイル: AlarmReceiver.cs プロジェクト: valero-col/Astropix
 public override void OnReceive(Context context, Intent intent)
 {
     Toast.MakeText(context, "Its time to download the data", ToastLength.Short).Show();
     try
     {
         ThreadPool.QueueUserWorkItem(async m =>
         {
             var httpClient = new HttpClient();
             var result     = await httpClient.GetStringAsync(urlplussecret);
             var post       = JsonConvert.DeserializeObject <ImageOfTheDay>(result);
             imageOfTheDay  = post;
             using (var dbhelper = new DBHelper())
             {
                 dbhelper.InsertIntoTableImageOfTheDay(imageOfTheDay);
                 if (post.Media_Type == "image")
                 {
                     WallpaperManager wallpaperManager = WallpaperManager.GetInstance(Application.Context);
                     wallpaperManager.SetBitmap(ImageComposer.RetrieveImagey(post.Hdurl));
                 }
             }
         });
     }
     catch
     {
         //Failed download
     }
 }
コード例 #2
0
        public override bool OnStartJob(JobParameters @params)
        {
            Log.Info("Astropix", "Its time to download data");
            try
            {
                ThreadPool.QueueUserWorkItem(async m =>
                {
                    //FIX ME: I crash when an HTTP Error occurs. (-:
                    var httpClient = new HttpClient();
                    var result     = await httpClient.GetStringAsync(urlplussecret);
                    var post       = JsonConvert.DeserializeObject <ImageOfTheDay>(result);
                    imageOfTheDay  = post;
                    using (var dbhelper = new DBHelper())
                    {
                        //If this query returns false, then Insert the new registry to the database.
                        //and download the image from the url and set it as wallpaper.

                        //If returns true, it means that the registry already exists, and it won't do anything, to avoid
                        //inserting the same item twice or even more.
                        if (!dbhelper.SelectQueryImageOfTheDay(post.Hdurl))
                        {
                            dbhelper.InsertIntoTableImageOfTheDay(imageOfTheDay);
                            if (post.Media_Type == "image")
                            {
                                ImageComposer.SetDownloadedImageAsBackground(post.Url);
                            }
                        }
                    }
                });
            }
            catch
            {
                return(false); //Failed download, needs rescheduling.
            }

            return(true);
        }