Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to migrate one method from one class to another? (Android, Java)

I have this code in the mainactivity:

public class mainAct extends AppCompatActivity {
    ...
    ...

    private void df(String fn, URL url) {
        File file=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+"/" + fn);
        if(file.exists())
            return;
        else {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url + ""));
            request.setTitle(fn);
            request.setMimeType("application/text");
            request.allowScanningByMediaScanner();
            request.setAllowedOverMetered(true);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
            request.setNotificationVisibility(2);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOCUMENTS, fn);
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
        }
    }
}

And I want to migrate it to other class as static method like so:

public class SampClass extends AppCompatActivity {
    static void df(String fn, URL url) {
        File file=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+"/" + fn);
        if(file.exists())
            return;
        else {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url + ""));
            request.setTitle(fn);
            request.setMimeType("application/text");
            request.allowScanningByMediaScanner();
            request.setAllowedOverMetered(true);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
            request.setNotificationVisibility(2);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOCUMENTS, fn);
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
        }
    }
}

But for some reason the getSystemService(DOWNLOAD_SERVICE) is requiring me to make the df method not static.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Update:

I tried adding "context" context.getSystemService() as suggested in the comment. But it turns red asking me to do at least 1 of the ff:

- Create local variable 'context'
- Create field 'context' in 'SampClass'
- Create parameter 'context'
- Rename reference

>Solution :

Pass the context to the function parameter and use it to get the system service.

// Add context to the parameter
static void df(Context context, String fn, URL url) {
    File file=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+"/" + fn);
    if(file.exists())
        return;
    else {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url + ""));
        request.setTitle(fn);
        request.setMimeType("application/text");
        request.allowScanningByMediaScanner();
        request.setAllowedOverMetered(true);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
        request.setNotificationVisibility(2);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOCUMENTS, fn);
        // And use it here to get the system service
        DownloadManager dm = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading