? ?當(dāng)react-native項(xiàng)目中有版本更新需求時(shí)肄方,android會(huì)用到下載新版本需求疾牲,此時(shí)嘀韧,可以通過封裝Native Modules 去調(diào)用android原生DownloadManager來實(shí)現(xiàn)下載茎用,傳輸下載APK鏈接地址實(shí)現(xiàn)覆蓋安裝
在android->app->src->main->java->你的包名(com.test.app) 新建3個(gè)類
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);
//設(shè)置通知欄標(biāo)題
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
request.setMimeType("application/vnd.android.package-archive");
request.setTitle("weiling");
if(description==null||"".equals(description)){
description="目標(biāo)apk正在下載";
}
request.setDescription(description);
request.setAllowedOverRoaming(false);
//設(shè)置文件存放目錄
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<NativeModule> modules=new ArrayList<>();
@Override? ??
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
modules.add(new DownloadApk(reactContext));
return modules;
}
@Override??
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override? ?
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
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中注冊廣播監(jiān)聽
記得在MainApplication中增加調(diào)用new DownloadApkPackage(),
然后在APP首頁進(jìn)去加載判斷是否更新:第一個(gè)參數(shù)APK下載地址昌抠,第二個(gè)參數(shù)APK下載時(shí)候命名
NativeModules.DownloadApk.downloading(this.state.downUrl,"weiling.apk"),
OK就可以看到APK下載成功了患朱,