Download manager not working without internet access
Asked Answered
P

1

3

I have implemented http server on raspberry device which has it own network without access to internet. Now, I am developing Xamarin.Android application which needs to download files stored on server. When I connect to raspberry network there is no internet connection. When I try to download any file from server via http using Android DownloadManager instance nothing happens. There is no notification and no error at all. But when I change network in my phone to my local wifi I have internet access and then notification about downloading failure shows up.

It seems that DownloadManager do not work offline, but customer don't want internet connection on raspberry device.

I created my download manager instance based on:

Android: How to use download manager class?

Download Manager not working

Here is the code:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        LoadApplication(new App());

        if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted || ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != Android.Content.PM.Permission.Granted)
        {

            // this will request for permission when user has not granted permission for the app 
            ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, 1);
        }

        else
        {
            //Download Script

            DownloadManager downloadManager = (DownloadManager)GetSystemService(Context.DownloadService);
            Android.Net.Uri uri = Android.Net.Uri.Parse("http://192.168.43.147:8080/data/22f13b77-d3cc-4788-96b3-cf454d4c07b6_IMG_20191008_122456.jpg");
            DownloadManager.Request request = new DownloadManager.Request(uri);
            request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile);
            request.SetVisibleInDownloadsUi(true); request.SetNotificationVisibility(Android.App.DownloadVisibility.Visible);
            request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, uri.Path.Replace('\\', '/').Split('/').Last());
            downloadManager.Enqueue(request);
        }
    }
}

Server works and correctly serve files because I can download it via web browser on my laptop.

How can I achieve this without download manager, or how to force download manager to work offline?

Procedure answered 8/10, 2019 at 13:11 Comment(10)
request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile); Why Mobile also? That's internet.Selig
This is my fifth approach. I tried without those options and result was the same.Procedure
You can always download a file yourself with HttpUrlConnection.Selig
Sure, but I cannot save downloaded file in Downloads directory without using DownloadManager. Whenever I try to save there is no error and file is present in /data/apname/Downloads but it is not visible in download folder on android. Maybe I do something wrong but I thought that without DownloadManager will not able to do thatProcedure
If you use HttpUrlConnection you can store the file where you want if you use Storage Access Framework. Let the user choose the Download directory with ACTION_OPEN_DOCUMENT_TREE. Or let the user create or choose a file in the Download directory with ACTION_CRREATE_DOCUMENT or ACTION_OPEN_DOCUMENT.Selig
Besides that i have a hard time to believe that downloadmanager needs internet connection.Selig
/data/apname/Downloads you mean /data/data/apname/Downloads?Selig
#58288897Selig
http://192.168.43.147 Are you trying to download from an Android device acting as a hotspot? Id the Android device where your app is running connected to the same hotspot?Selig
But when I change network in my phone to my local wifi I have internet access You can let your local wifi on but detach the cable from the wall to the router. So no internet but wifi yes. Does it work?Selig
H
0

I tackled the same problem in an enterprise network with restricted network access. The way DownloadManager works is that it checks for internet connectivity before doing the download. It apparently does that via ConnectivityManager which uses a bunch of different techniques to do that. One of them is by making a call to a 'connectivitycheck' service and expecting a specific status code (204) as result. This is actually documented by google but it is annoyingly hard to find: link

connectivitycheck

Adding this exceptions to the firewall solved the internet connectivity problem.

For the specific case (raspberry) in the question posted I suppose that handling (faking) this 'request-response' interaction on the raspberry server should solve the problem.

Highgrade answered 26/6 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.