0

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;
    }

}
Smitakshi Medhi
  • 31
  • 1
  • 1
  • 3
  • 1
    `getSystemService` in a method of `Context` class. If you want to use it in a non-activity class, refer this: http://stackoverflow.com/a/4870714/4350275 – Prerak Sola Mar 23 '16 at 11:23
  • The error suggests that there is something wrong with getSystemService() method. Can you try this.getSystemService()? – Alok Gupta Mar 23 '16 at 11:24

2 Answers2

0

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

getContext().getSystemService()

Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35
0

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.

Pravin Divraniya
  • 4,223
  • 2
  • 32
  • 49