下载管理器在 Android Pie 9.0 NetworkSecurityConfig 中不起作用:未指定网络安全配置,使用平台默认值
Posted
技术标签:
【中文标题】下载管理器在 Android Pie 9.0 NetworkSecurityConfig 中不起作用:未指定网络安全配置,使用平台默认值【英文标题】:Download Manger not working in Android Pie 9.0 NetworkSecurityConfig: No Network Security Config specified, using platform default 【发布时间】:2019-04-28 19:18:21 【问题描述】:在将设备操作系统更新为 android 9.0
时,之前此代码运行良好(小米米 A2)。现在,Android Pie 9.0
上没有下载文件。
此外,它在奥利奥、牛轧糖、棉花糖中运行良好
这里是sn-p的代码:
File myDir = new File(Environment.getExternalStorageDirectory(), "MyApp");
if (!myDir.exists())
myDir.mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE).setAllowedOverMetered(true)
.setAllowedOverRoaming(true).setTitle("Downloading demo file").
setVisibleInDownloadsUi(true)
.setDestinationInExternalPublicDir(folder_main + "/", timeStamp);
mgr.enqueue(request);
logcat 中的错误:
11-27 11:17:28.056 4062-2814/? D/DownloadManager: [3970] Starting
11-27 11:17:28.067 4062-2814/? W/DownloadManager: [3970] Stop requested with status HTTP_DATA_ERROR
11-27 11:17:28.068 4062-2814/? D/DownloadManager: [3970] Finished with status WAITING_TO_RETRY
11-27 11:18:06.466 4062-2843/? D/DownloadManager: [3970] Starting
11-27 11:18:06.576 4062-2843/? W/DownloadManager: [3970] Stop requested with status HTTP_DATA_ERROR
11-27 11:18:06.577 4062-2843/? D/DownloadManager: [3970] Finished with status WAITING_TO_RETRY
11-27 11:19:06.581 4062-2867/? D/DownloadManager: [3968] Starting
11-27 11:19:06.737 4062-2867/? W/DownloadManager: [3968] Stop requested with status HTTP_DATA_ERROR
11-27 11:19:06.738 4062-2867/? D/DownloadManager: [3968] Finished with status WAITING_TO_RETRY
11-27 11:19:19.131 4062-2869/? D/DownloadManager: [3970] Starting
11-27 11:19:19.144 4062-2869/? W/DownloadManager: [3970] Stop requested with status HTTP_DATA_ERROR
11-27 11:19:19.144 4062-2869/? D/DownloadManager: [3970] Finished with status WAITING_TO_RETRY
11-27 11:19:36.243 4062-2872/? D/DownloadManager: [3969] Starting
11-27 11:19:36.259 4062-2872/? W/DownloadManager: [3969] Stop requested with status HTTP_DATA_ER
任何帮助将不胜感激。谢谢
【问题讨论】:
【参考方案1】:在Xiaomi mi A2
今天收到软件更新通知后,这对我有用。
什么对我有用
在application
标签中添加android:networkSecurityConfig="@xml/network_security_config"
<application
android:name=".ApplicationClass"
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:theme="@style/AppTheme">
在哪里network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
在res
目录下创建xml
,然后在xml
文件夹中创建network_security_config.xml
,如下图所示
This explains the issue in software
【讨论】:
这个问题有时会再次出现。你遇到过同样的问题吗? 是的,有时...可能与操作系统有关...您在哪个设备上进行测试?【参考方案2】:Use this Service class
public class DownloadService extends IntentService
public DownloadService()
super("Download Service");
String CHANNEL_ID1 = "Download Notification";
String CHANNEL_NAME1 = "Download Notification";
private NotificationManager notificationManager;
private int totalFileSize;
String name;
String url;
NotificationCompat.Builder builder;
@Override
protected void onHandleIntent(Intent intent)
name = intent.getStringExtra("name");
url = intent.getStringExtra("url");
createChannels();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sounduri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder = new NotificationCompat.Builder(this, CHANNEL_ID1);
builder.setContentTitle("Download");
builder.setContentText("Downloading File");
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.mipmap.ic_launcher);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(bm);
builder.setSound(null);
builder.setAutoCancel(true);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
initDownload();
private void initDownload()
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("Your Url") //Url Base here
.build();
RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);
Call<ResponseBody> request = retrofitInterface.downloadFile(url);// pass your Url
try
downloadFile(request.execute().body());
catch (IOException e)
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
private void downloadFile(ResponseBody body) throws IOException
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder name";
File folder = new File(path);
if (!folder.exists())
folder.mkdir();
int count;
byte data[] = new byte[1024 * 4];
long fileSize = body.contentLength();
InputStream bis = new BufferedInputStream(body.byteStream(), 1024 * 8);
File outputFile = new File(path, name + ".mp3");// file name & extension
OutputStream output = new FileOutputStream(outputFile);
long total = 0;
long startTime = System.currentTimeMillis();
int timeCount = 1;
while ((count = bis.read(data)) != -1)
total += count;
totalFileSize = (int) (fileSize / (Math.pow(1024, 2)));
double current = Math.round(total / (Math.pow(1024, 2)));
int progress = (int) ((total * 100) / fileSize);
long currentTime = System.currentTimeMillis() - startTime;
Download download = new Download();
download.setTotalFileSize(totalFileSize);
if (currentTime > 1000 * timeCount)
download.setCurrentFileSize((int) current);
download.setProgress(progress);
sendNotification(download);
timeCount++;
output.write(data, 0, count);
onDownloadComplete();
output.flush();
output.close();
bis.close();
private void sendNotification(Download download)
builder.setProgress(100, download.getProgress(), false);
builder.setContentText("Downloading file " + download.getCurrentFileSize() + "/" + totalFileSize + " MB");
notificationManager.notify(0, builder.build());
private void onDownloadComplete()
Download download = new Download();
download.setProgress(100);
notificationManager.cancel(0);
builder.setProgress(0, 0, false);
builder.setContentText("File Downloaded");
//builder.setVibrate(null);
notificationManager.notify(0, builder.build());
@Override
public void onTaskRemoved(Intent rootIntent)
notificationManager.cancel(0);
public void createChannels()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel notifiCationChannel = new NotificationChannel(CHANNEL_ID1, CHANNEL_NAME1, NotificationManager.IMPORTANCE_DEFAULT);
notifiCationChannel.enableLights(true);
notifiCationChannel.enableVibration(false);
notifiCationChannel.setLightColor(Color.GREEN);
notifiCationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notifiCationChannel.setShowBadge(true);
notifiCationChannel.setSound(null,null);
notificationManager.createNotificationChannel(notifiCationChannel);
从您的活动/片段中调用此方法并传递 url 和名称
private fun startDownload()
val intent = Intent(this, DownloadService::class.java)
intent.putExtra("name", name)
intent.putExtra("url", url)
startService(intent)
Retrofit Interface
interface RetrofitInterface
@GET
@Streaming
fun downloadFile(@Url url: String): Call<ResponseBody>
【讨论】:
感谢您的代码,但我希望使用下载管理器类以上是关于下载管理器在 Android Pie 9.0 NetworkSecurityConfig 中不起作用:未指定网络安全配置,使用平台默认值的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 9.0(PIE) 中获取 WIFI SSID?
多进程中的 Android Pie (9.0) WebView
FusedLocationProviderClient 在 Android Pie 9.0 中返回错误的纬度经度