改成下载版
This commit is contained in:
parent
c95bb7523a
commit
d5ec06c7ab
@ -2,13 +2,8 @@
|
|||||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
|
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||||
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||||
<application>
|
<application>
|
||||||
<receiver android:name=".DownLoadBroadcastReceiver">
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
|
|
||||||
</intent-filter>
|
|
||||||
</receiver>
|
|
||||||
<provider
|
<provider
|
||||||
android:name=".VersionUpgradeFileProvider"
|
android:name=".VersionUpgradeFileProvider"
|
||||||
android:authorities="${applicationId}.fileprovider"
|
android:authorities="${applicationId}.fileprovider"
|
||||||
|
@ -1,52 +0,0 @@
|
|||||||
package top.jerryyan.RN.A.VersionUpgrade;
|
|
||||||
|
|
||||||
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.Build;
|
|
||||||
import androidx.core.content.FileProvider;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
public class DownLoadBroadcastReceiver extends BroadcastReceiver {
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
long currentDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
|
||||||
SharedPreferences sPreferences = context.getSharedPreferences("jerry_rn_a_version_upgrade", 0);
|
|
||||||
long reference = sPreferences.getLong("downloadId", 0);
|
|
||||||
if (currentDownloadId == reference) {
|
|
||||||
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
||||||
Intent install = new Intent(Intent.ACTION_VIEW);
|
|
||||||
DownloadManager.Query queryById = new DownloadManager.Query();
|
|
||||||
queryById.setFilterById(currentDownloadId);
|
|
||||||
Cursor cursor = downloadManager.query(queryById);
|
|
||||||
String fileUri = null;
|
|
||||||
if(cursor.moveToFirst()){
|
|
||||||
if(cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL){
|
|
||||||
fileUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
|
|
||||||
}else{
|
|
||||||
downloadManager.remove(currentDownloadId);
|
|
||||||
cursor.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
if(fileUri == null) return;
|
|
||||||
Uri fileUri1 = Uri.parse(fileUri);
|
|
||||||
File file = new File(fileUri1.getPath());
|
|
||||||
if (Build.VERSION.SDK_INT >= 24) {
|
|
||||||
Uri apkUri = FileProvider.getUriForFile(context, context.getPackageName().concat(".fileprovider"), file);
|
|
||||||
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
||||||
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
|
|
||||||
} else {
|
|
||||||
install.setDataAndType(Uri.parse(fileUri), "application/vnd.android.package-archive");
|
|
||||||
}
|
|
||||||
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
||||||
context.startActivity(install);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +1,38 @@
|
|||||||
package top.jerryyan.RN.A.VersionUpgrade;
|
package top.jerryyan.RN.A.VersionUpgrade;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.DownloadManager;
|
import android.app.AlertDialog;
|
||||||
import android.content.Context;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Environment;
|
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.facebook.react.bridge.Callback;
|
|
||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
import com.facebook.react.bridge.ReactApplicationContext;
|
||||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||||
import com.facebook.react.bridge.ReactMethod;
|
import com.facebook.react.bridge.ReactMethod;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.Callback;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class UpgradeModule extends ReactContextBaseJavaModule {
|
public class UpgradeModule extends ReactContextBaseJavaModule {
|
||||||
private final ReactApplicationContext reactContext;
|
private final ReactApplicationContext reactContext;
|
||||||
private String downloadUrl;
|
private String DOWNLOAD_URL = "http://luntan.qgmzbxs.com/source/tsgzCircles.apk";
|
||||||
private String title = "更新";
|
private String TITLE = "更新";
|
||||||
|
private String CONTENT = "大家好,我是勤劳的催更新菌\n点击更新,让我们一起成为一天更新八次的暴躁老哥吧";
|
||||||
|
|
||||||
public UpgradeModule(ReactApplicationContext reactContext) {
|
public UpgradeModule(ReactApplicationContext reactContext) {
|
||||||
super(reactContext);
|
super(reactContext);
|
||||||
@ -39,16 +50,6 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
|
|||||||
return constants;
|
return constants;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReactMethod
|
|
||||||
public void setDownloadUrl(String downloadUrl) {
|
|
||||||
this.downloadUrl = downloadUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
@ReactMethod
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean _hasPermissionToInstall() {
|
private boolean _hasPermissionToInstall() {
|
||||||
Activity activity = reactContext.getCurrentActivity();
|
Activity activity = reactContext.getCurrentActivity();
|
||||||
return activity != null && (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || activity.getPackageManager().canRequestPackageInstalls());
|
return activity != null && (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || activity.getPackageManager().canRequestPackageInstalls());
|
||||||
@ -64,21 +65,91 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void _alertDialog(){
|
||||||
|
final UpgradeModule that = this;
|
||||||
|
AlertDialog dialog = new AlertDialog.Builder(reactContext.getCurrentActivity())
|
||||||
|
.setTitle(TITLE)
|
||||||
|
.setMessage(CONTENT)
|
||||||
|
.setNeutralButton("取消",
|
||||||
|
new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.setNegativeButton("更新",
|
||||||
|
new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
that._startDownload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.show();
|
||||||
|
dialog.setCanceledOnTouchOutside(true);//可选
|
||||||
|
dialog.setCancelable(true);//可选
|
||||||
|
}
|
||||||
|
|
||||||
|
private void _startDownload(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void startDownload() {
|
public void checkUpgrade(){
|
||||||
Activity activity = reactContext.getCurrentActivity();
|
final UpgradeModule that = this;
|
||||||
if (activity == null) return;
|
OkHttpClient okHttpClient = new OkHttpClient();
|
||||||
if (!this._hasPermissionToInstall()) this._requestInstallPermission();
|
String upgradeUrl = "https://www.tsgzvore.com/versionCheck";
|
||||||
DownloadManager downloadManager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
|
PackageManager manager = reactContext.getPackageManager();
|
||||||
Uri uri = Uri.parse(downloadUrl);
|
try {
|
||||||
DownloadManager.Request request = new DownloadManager.Request(uri);
|
String packageName = reactContext.getPackageName();
|
||||||
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
|
PackageInfo info = manager.getPackageInfo(packageName, 0);
|
||||||
request.setMimeType("application/vnd.android.package-archive");
|
String versionName = info.versionName;
|
||||||
request.setTitle(this.title);
|
upgradeUrl = upgradeUrl
|
||||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
|
.concat("?packageName=")
|
||||||
request.setDestinationInExternalFilesDir(activity, Environment.DIRECTORY_DOWNLOADS, "");
|
.concat(packageName)
|
||||||
long downloadId = downloadManager.enqueue(request);
|
.concat("&version=")
|
||||||
SharedPreferences sharedPreferences = activity.getSharedPreferences("jerry_rn_a_version_upgrade", 0);
|
.concat(versionName);
|
||||||
sharedPreferences.edit().putLong("downloadId", downloadId).commit();
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
|
Log.e("Version Upgrade", "NOT FOUND MY PACKAGE TAT", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final Request request = new Request.Builder()
|
||||||
|
.url(upgradeUrl)
|
||||||
|
.method("GET", null)
|
||||||
|
.build();
|
||||||
|
Call call = okHttpClient.newCall(request);
|
||||||
|
call.enqueue(new Callback() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call call, IOException e) {
|
||||||
|
Log.e("VERSION CHECK", call.toString(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call call, Response response) {
|
||||||
|
Log.d("VERSION CHECK", call.toString());
|
||||||
|
if(response.isSuccessful()){
|
||||||
|
Log.d("VERSION CHECK", "REQUESTS OK");
|
||||||
|
if(response.body() == null) return;
|
||||||
|
try {
|
||||||
|
String jsonText = response.body().string();
|
||||||
|
Log.d("VERSION CHECK", jsonText);
|
||||||
|
JSONObject json = new JSONObject(jsonText);
|
||||||
|
int status = json.getInt("status");
|
||||||
|
CONTENT = json.getString("message");
|
||||||
|
String version = json.getString("version");
|
||||||
|
if(status > 0){
|
||||||
|
JSONObject jsonData = json.getJSONObject("data");
|
||||||
|
DOWNLOAD_URL = jsonData.getString("url");
|
||||||
|
that._alertDialog();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e("REQUEST FAILED", "AAA", e);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.e("JSON FAILED", "BBB", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user