当react-native项目中有版本更新需求时,android会用到下载新版本需求,此时,可以通过封装Native Modules 去调用android原生DownloadManager来实现下载,传输下载APK链接地址实现覆盖安装
在android->app->src->main->java->你的包名(com.test.app) 新建3个类
1.DownloadApk.java
package com.test.app;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.app.Activity;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Environment;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
public class DownloadApk extends ReactContextBaseJavaModule{
DownloadManager downManager ;
Activity myActivity;
public DownloadApk(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public boolean canOverrideExistingModule() {
return true;
}
@Override
public String getName(){
return "DownloadApk";
}
@ReactMethod
public void downloading(String url,String description){
myActivity =getCurrentActivity();
downManager = (DownloadManager)myActivity.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new Request(uri);
request.setAllowedNetworkTypes(Request.NETWORK_WIFI);
//设置通知栏标题
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
request.setMimeType("application/vnd.android.package-archive");
request.setTitle("weiling");
if(description==null||"".equals(description)){
description="目标apk正在下载";
}
request.setDescription(description);
request.setAllowedOverRoaming(false);
//设置文件存放目录
request.setDestinationInExternalFilesDir(myActivity, Environment.DIRECTORY_DOWNLOADS, description);
long downloadid=downManager.enqueue(request);
SharedPreferences sPreferences = myActivity.getSharedPreferences("ggfw_download", 0);
sPreferences.edit().putLong("ggfw_download_apk", downloadid).commit();
}
}
2.DownloadApkPackage.java
package com.test.app;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
public class DownloadApkPackage implements ReactPackage {
List
@Override
public List
modules.add(new DownloadApk(reactContext));
return modules;
}
@Override
public List
return Collections.emptyList();
}
@Override
public List
return Collections.emptyList();
}
}
3.DownLoadBroadcastReceiver.java
package com.test.app;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
public class DownLoadBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
long myDwonloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
SharedPreferences sPreferences = context.getSharedPreferences("ggfw_download", 0);
long refernece = sPreferences.getLong("ggfw_download_apk", 0);
if (refernece == myDwonloadID) {
DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Intent install = new Intent(Intent.ACTION_VIEW);
// Uri downloadFileUri = dManager.getUriForDownloadedFile(myDwonloadID);
DownloadManager.Query querybyId = new DownloadManager.Query();
querybyId.setFilterById(myDwonloadID);
Cursor myDownload = dManager.query(querybyId);
String dolownname=null;
if(myDownload.moveToFirst()){
int status = myDownload.getInt(myDownload.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
// process download
int fileNameIdx = myDownload.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
//此处取得的是完整路径+文件名称
dolownname=myDownload.getString(fileNameIdx);
}else{
Toast.makeText(context,"下载失败,删除残留文件",Toast.LENGTH_LONG).show();
dManager.remove(myDwonloadID);
myDownload.close();
return;
}
myDownload.close();
}
if(dolownname==null){
return;
}
File file = new File(dolownname);
install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(install);
}}}
记得在AndroidManifest.xml中注册广播监听
记得在MainApplication中增加调用new DownloadApkPackage(),
然后在APP首页进去加载判断是否更新:第一个参数APK下载地址,第二个参数APK下载时候命名
NativeModules.DownloadApk.downloading(this.state.downUrl,"weiling.apk"),
OK就可以看到APK下载成功了,