我是否试图从我的Web View Android应用程序下载文件? 我是android开发的新手。而且我不知道我的代码有什么问题。
*
错误:(31,57)错误:找不到符号方法 getSystemService(String)错误:任务执行失败 ':应用程序: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;
}
}
答案 0 :(得分:0)
getSystemService方法将无法工作。 试试
的getContext()。getSystemService()
答案 1 :(得分:0)
对于非活动类(如您的情况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);
希望这会对你有所帮助。