【问题标题】:Downloading files from Web View Android app从 Web View Android 应用程序下载文件
【发布时间】:2016-03-23 11:19:20
【问题描述】:

我正在尝试从我的 Web View Android 应用下载文件? 我是android开发的新手。而且我不知道我的代码有什么问题。

*

错误:(31, 57) 错误: 找不到符号方法 getSystemService(String) 错误:任务执行失败 ':app:compileDebugJavaWithJavac'.

编译失败;有关详细信息,请参阅编译器错误输出。

*

这是我的代码: 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;
    }

}

【问题讨论】:

  • getSystemServiceContext 类的方法中。如果你想在非活动类中使用它,请参考:stackoverflow.com/a/4870714/4350275
  • 错误提示 getSystemService() 方法有问题。你可以试试 this.getSystemService() 吗?

标签: java android webview


【解决方案1】:

getSystemService 方法在非活动类中没有上下文将无法工作。 试试

getContext().getSystemService()

【讨论】:

    【解决方案2】:

    对于您的情况MyAppWebViewClient 中的非活动类,如果我们想访问 Context 类的方法,我们必须使用 Context 类实例。所以最好的方法是创建一个构造函数并在参数中传递 Context 类实例。

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

    每当您从活动类调用MyAppWebViewClient 类时,只需在创建同一类的新实例时传递活动上下文。喜欢

    MyAppWebViewClient myAppWebViewClient = new MyAppWebViewClient(YourActivityName.this);
    

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多