我在应用程序的webview中加载了url,但是在点击下载按钮时没有在webview中下载功能,请给我解决方案。
Public class FinancialActivity extends AppCompatActivity {
WebView webview;
public static final String SHARED_PREF_NAME = "myloginapp";
SharedPreferences sharedPreferences;
String id, studid, degree, user_type;
String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_financial);
sharedPreferences = FinancialActivity.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
studid = sharedPreferences.getString(LoginActivity.ID_SHARED_PREF, "Not Available");
degree = sharedPreferences.getString(LoginActivity.KEY_DEGREE_SOUGHT, "Not Available");
user_type = sharedPreferences.getString("user_type", "Not Available");
url = Urlinfo.Financial + "stud_id=" + studid + "&user_type=" + user_type + "&subscription_status=" + DrawerActivity.subvalue + "&stud_degree=" + degree;
Log.d("urlapplication", "" + url);
WebView w = new WebView(this);
w.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
w.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
setContentView(w);
w.loadUrl(url);
}
}
}
答案 0 :(得分:0)
在这里尝试以下代码
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// handle different requests for different type of files
// this example handles downloads requests for .apk and .mp3 files
// everything else the webview can handle normally
if (url.endsWith(".apk")) { //change .apk to any format you want to download
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);
}
else if(url.endsWith(".mp3")) {
// if the link points to an .mp3 resource do something else
}
// if there is a link to anything else than .apk or .mp3 load the URL in the webview
else view.loadUrl(url);
return true;
}
希望这有帮助