I have an react-native app runs on native android9, now I want to use DownloadManager to download an apk file, when download is begining, it doesn't show notification immediately, no downloading progress too, but waiting for many minutes it shows download failed. I have set fileprovider and networkSecurityConfig and static broadcast in AndroidManifest.xml.AndroidManifest.xml
...<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/><application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:networkSecurityConfig="@xml/network_security_config" android:theme="@style/AppTheme"><uses-library android:name="org.apache.http.legacy" android:required="false" /><activity android:name=".MainActivity" ...><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:grantUriPermissions="true" android:exported="false"><meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /></provider><receiver android:name="com.cityvoice.cubebrain.MyBroadcastReceiver"><intent-filter><action android:name="android.intent.action.DOWNLOAD_COMPLETE"/></intent-filter></receiver></application>
...file_paths.xml
<?xml version="1.0" encoding="utf-8"?><resources><paths><external-files-path path="" name="Download" /></paths></resources>
network_security_config.xml
<?xml version="1.0" encoding="utf-8"?><network-security-config><base-config cleartextTrafficPermitted="true"/></network-security-config>
UpdateManager.java
public class UpdateManager extends ReactContextBaseJavaModule implements ActivityEventListener{ ... public UpdateManager(ReactApplicationContext reactContext) { super(reactContext); mContext = reactContext; reactContext.addActivityEventListener(this); } @ReactMethod public void update(String downloadPath) { String[] arrPaths = downloadPath.split("/"); if(arrPaths.length>0){ DOWNLOAD_PATH = downloadPath; APK_NAME = arrPaths[arrPaths.length-1]; Log.d(TAG, "update >>>>>>>>>>>>>>>>>>"+ downloadPath); File mFile = new File(mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), APK_NAME); new AndroidDownloadManager(mContext, downloadPath, APK_NAME) .setListener(new AndroidDownloadManagerListener() { @Override public void onPrepare() { Log.d(TAG, "onPrepare"); } @Override public void onSuccess(String path) { Log.d(TAG, "onSuccess >>>>"+ path); new InstallUtil(getCurrentActivity(), mFile.getAbsolutePath()).install(); } @Override public void onFailed(Throwable throwable) { Log.e(TAG, "onFailed", throwable); } }) .download(); } }}
MyBroadcastReceiver.java
...public class MyBroadcastReceiver extends BroadcastReceiver { private final String TAG = "TEST-MyTestReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG,"Static receiver:action="+ intent.getAction()); }}
AndroidDownloadManager.java
public class AndroidDownloadManager { ... public AndroidDownloadManager(Context context, String url, String name) { this.context = context; this.url = url; this.name = name; } public AndroidDownloadManager setListener(AndroidDownloadManagerListener listener) { this.listener = listener; return this; } public void download() { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setAllowedOverRoaming(false); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setTitle(name); request.setDescription("downloading......"); request.setVisibleInDownloadsUi(true); File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), name); request.setDestinationUri(Uri.fromFile(file)); path = file.getAbsolutePath(); if (downloadManager == null) { downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); } if (downloadManager != null) { if (listener != null) { listener.onPrepare(); } downloadId = downloadManager.enqueue(request); } context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } private MyBroadcastReceiver receiver = new MyBroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { DownloadManager.Query query = new DownloadManager.Query(); Log.d(TAG, "onReceive >>>>"+ url); query.setFilterById(downloadId); Cursor cursor = downloadManager.query(query); if (cursor.moveToFirst()) { int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); switch (status) { case DownloadManager.STATUS_PAUSED: break; case DownloadManager.STATUS_PENDING: break; case DownloadManager.STATUS_RUNNING: break; case DownloadManager.STATUS_SUCCESSFUL: if (listener != null) { listener.onSuccess(path); } cursor.close(); context.unregisterReceiver(receiver); break; case DownloadManager.STATUS_FAILED: if (listener != null) { listener.onFailed(new Exception("failed")); } cursor.close(); context.unregisterReceiver(receiver); break; } } } };}
waiting for mor than 10 minutes it throws download failed as below, no further information, what the reason? It has took me many days.
D/download: update >>>>>>>>>>>>>>>>>>http://.../xxx.apkD/download: onPrepareI/voice.cubebrai: Background concurrent copying GC freed 67618(5MB) AllocSpace objects, 0(0B) LOS objects, 49% free, 5MB/10MB, paused 323us total 106.962msD/download: onReceive >>>>http://.../xxx.apkE/download: onFailed java.lang.Exception: failed at com.cityvoice.cubebrain.AndroidDownloadManager$1.onReceive(AndroidDownloadManager.java:118) at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1391) at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2) at android.os.Handler.handleCallback(Handler.java:874) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:198) at android.app.ActivityThread.main(ActivityThread.java:6729) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)