简体   繁体   中英

Downloading files from Web View Android app

I'm trying to download files from my Web View Android app? I'm newbie to android development. And I've no idea whats wrong with my code.

*

Error:(31, 57) error: cannot find symbol method getSystemService(String) Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

*

Here is my code: MyAppWebViewClient.java

public class MyAppWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.contains(".apk")) {
            Uri source = Uri.parse(url);
            // Make a new request pointing to the .apk url
            DownloadManager.Request request = new DownloadManager.Request(source);
            // appears the same in Notification bar while downloading
            request.setDescription("Description for the DownloadManager Bar");
            request.setTitle("YourApp.apk");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            }
            // save the file in the "Downloads" folder of SDCARD
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk");
            // get download service and enqueue file
            DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            manager.enqueue(request);
            return true;
        }

        if (Uri.parse(url).getHost().contains("mysite.com")) {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }

}

getSystemService method will not work without context in Non-Activity Class. Try

getContext().getSystemService()

For Non-Activity classes as in your case MyAppWebViewClient if we want to access methods of Context class we have to do so using Context class instance. So the best way to do so is create a constructor and pass Context class instance in parameter.

public class MyAppWebViewClient extends WebViewClient {
private final Context context;

public MyAppWebViewClient(Context context){
this.context = context;
}
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.contains(".apk")) {
            Uri source = Uri.parse(url);
            // Make a new request pointing to the .apk url
            DownloadManager.Request request = new DownloadManager.Request(source);
            // appears the same in Notification bar while downloading
            request.setDescription("Description for the DownloadManager Bar");
            request.setTitle("YourApp.apk");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            }
            // save the file in the "Downloads" folder of SDCARD
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk");
            // get download service and enqueue file
            DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            manager.enqueue(request);
            return true;
        }

        if (Uri.parse(url).getHost().contains("mysite.com")) {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }

}

Whenever you call MyAppWebViewClient class from activity class, just pass activity context when you create a new instance of the same class. Like

MyAppWebViewClient myAppWebViewClient = new MyAppWebViewClient(YourActivityName.this);

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM