Не работает загрузка файла через URL с помощью Download Manager (API 29)
Использую этот пример, но при нажатию на кнопку ничего не происходит, даже в логах. В чем может быть проблема или как её выявить?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download Content" />
</LinearLayout>
MainActivity
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button button;
DownloadManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.download);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
long reference = manager.enqueue(request);
}
});
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Ответы (1 шт):
Автор решения: Sergey
→ Ссылка
Проблема была в
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
Нужно заменить на:
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);