改成下载版
This commit is contained in:
parent
c95bb7523a
commit
d5ec06c7ab
@ -2,13 +2,8 @@
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
|
||||
<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>
|
||||
<receiver android:name=".DownLoadBroadcastReceiver">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<provider
|
||||
android:name=".VersionUpgradeFileProvider"
|
||||
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;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.DownloadManager;
|
||||
import android.content.Context;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
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.ReactContextBaseJavaModule;
|
||||
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.Map;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class UpgradeModule extends ReactContextBaseJavaModule {
|
||||
private final ReactApplicationContext reactContext;
|
||||
private String downloadUrl;
|
||||
private String title = "更新";
|
||||
private String DOWNLOAD_URL = "http://luntan.qgmzbxs.com/source/tsgzCircles.apk";
|
||||
private String TITLE = "更新";
|
||||
private String CONTENT = "大家好,我是勤劳的催更新菌\n点击更新,让我们一起成为一天更新八次的暴躁老哥吧";
|
||||
|
||||
public UpgradeModule(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
@ -39,16 +50,6 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
|
||||
return constants;
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void setDownloadUrl(String downloadUrl) {
|
||||
this.downloadUrl = downloadUrl;
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
private boolean _hasPermissionToInstall() {
|
||||
Activity activity = reactContext.getCurrentActivity();
|
||||
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
|
||||
public void startDownload() {
|
||||
Activity activity = reactContext.getCurrentActivity();
|
||||
if (activity == null) return;
|
||||
if (!this._hasPermissionToInstall()) this._requestInstallPermission();
|
||||
DownloadManager downloadManager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
Uri uri = Uri.parse(downloadUrl);
|
||||
DownloadManager.Request request = new DownloadManager.Request(uri);
|
||||
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
|
||||
request.setMimeType("application/vnd.android.package-archive");
|
||||
request.setTitle(this.title);
|
||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
|
||||
request.setDestinationInExternalFilesDir(activity, Environment.DIRECTORY_DOWNLOADS, "");
|
||||
long downloadId = downloadManager.enqueue(request);
|
||||
SharedPreferences sharedPreferences = activity.getSharedPreferences("jerry_rn_a_version_upgrade", 0);
|
||||
sharedPreferences.edit().putLong("downloadId", downloadId).commit();
|
||||
public void checkUpgrade(){
|
||||
final UpgradeModule that = this;
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
String upgradeUrl = "https://www.tsgzvore.com/versionCheck";
|
||||
PackageManager manager = reactContext.getPackageManager();
|
||||
try {
|
||||
String packageName = reactContext.getPackageName();
|
||||
PackageInfo info = manager.getPackageInfo(packageName, 0);
|
||||
String versionName = info.versionName;
|
||||
upgradeUrl = upgradeUrl
|
||||
.concat("?packageName=")
|
||||
.concat(packageName)
|
||||
.concat("&version=")
|
||||
.concat(versionName);
|
||||
} 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