public static DownloadReturns ExecuteDownload(Context context, RssEnCaso rssEnCaso, bool onlyWiFi, bool isManual) { if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.M) { Permission permissionCheck = context.CheckSelfPermission(Manifest.Permission.WriteExternalStorage); if (permissionCheck != Permission.Granted) { return(DownloadReturns.NoWritePermission); } } NetworkDetection networkDetection = NetworkDetection.DetectNetwork(context); if (!networkDetection.IsOnline) { return(DownloadReturns.NoInternetConection); } if (onlyWiFi && !networkDetection.IsWifi) { return(DownloadReturns.NoWiFiFound); } Intent downloadIntent = new Intent(context, typeof(DownloadService)); // Flag to start service if (isManual) { downloadIntent.SetAction(General.DOWNLOAD_START_SERVICE); } else { downloadIntent.SetAction(General.DOWNLOAD_START_AUTO); } // Passing values to intent downloadIntent.PutExtra("file", rssEnCaso.Url); downloadIntent.PutExtra("audioboomurl", rssEnCaso.AudioBoomUrl); downloadIntent.PutExtra("title", rssEnCaso.Title); downloadIntent.PutExtra("description", rssEnCaso.Description); downloadIntent.PutExtra("imageurl", rssEnCaso.ImageUrl); downloadIntent.PutExtra("pubdate", rssEnCaso.PubDate.ToString()); // Starting service context.StartService(downloadIntent); return(DownloadReturns.ServiceStarted); }
public static NetworkDetection DetectNetwork(Context context) { NetworkDetection networkDetection = new NetworkDetection(); ConnectivityManager connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService); var networkCapabilities = connectivityManager.GetNetworkCapabilities(connectivityManager.ActiveNetwork); if (networkCapabilities.HasCapability(NetCapability.Internet)) { networkDetection.isOnline = true; if (networkCapabilities.HasTransport(TransportType.Wifi)) { networkDetection.isWifi = true; } if (networkCapabilities.HasTransport(TransportType.Cellular)) { networkDetection.isMobile = true; } } return(networkDetection); }