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

Time:08-13

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.

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

CodePudding user response:

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);
    }
}
  • Related