【发布时间】: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;
}
}
【问题讨论】:
-
getSystemService在Context类的方法中。如果你想在非活动类中使用它,请参考:stackoverflow.com/a/4870714/4350275 -
错误提示 getSystemService() 方法有问题。你可以试试 this.getSystemService() 吗?