update:2020.03.16 加入更新升级框架

This commit is contained in:
2020-03-16 18:11:18 +08:00
parent 5e8d5d12e0
commit e898f83670
5 changed files with 40 additions and 12 deletions

View File

@@ -136,7 +136,6 @@ public class MainActivity extends AppCompatActivity {
mHits = new long[COUNTS];//重新初始化数组
Toast.makeText(this, "正在检查更新", Toast.LENGTH_SHORT).show();
HTTPInterface.checkUpdateByPackage(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE));
}
}
@@ -286,6 +285,7 @@ public class MainActivity extends AppCompatActivity {
}
}
};
private void getFile(final Bundle bundle) {
final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/");
path.mkdirs();

View File

@@ -15,7 +15,6 @@ import android.util.Log;
import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.task.DownloadTask;
//import com.blankj.utilcode.util.NetworkUtils;
import com.info.sn.network.api.HTTPInterface;
import com.info.sn.utils.ApkUtils;
import com.info.sn.utils.LogUtils;
@@ -33,6 +32,8 @@ import java.util.TreeMap;
import cn.jpush.android.api.CustomMessage;
//import com.blankj.utilcode.util.NetworkUtils;
//public class MyApplication extends Application implements NetworkUtils.OnNetworkStatusChangedListener {
public class MyApplication extends Application {
public static Context context;
@@ -375,7 +376,11 @@ public class MyApplication extends Application {
String url = extra.getString("url");
File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/apk");
file.mkdirs();
Aria.download(this).load(url).setFilePath(file.getAbsolutePath() + "/" + packages + ".apk").ignoreFilePathOccupy().setExtendField(packages).create();
Aria.download(this)
.load(url)
.setFilePath(file.getAbsolutePath() + "/" + packages + ".apk")
.ignoreFilePathOccupy()
.setExtendField(packages).create();
// OkGo.<File>get(url)
// .execute(new FileCallback() {
// @Override

View File

@@ -28,7 +28,6 @@ import java.util.List;
import java.util.Random;
public class HTTPInterface {
private final static int requestCodeOK = 200;
//获取设备信息接口
public static synchronized void checkDevicesInfo(final Handler handler) {
@@ -46,7 +45,7 @@ public class HTTPInterface {
UserInfo userInfo = JSON.parseObject(data, UserInfo.class);
Message message = new Message();
message.obj = userInfo;
if (code == requestCodeOK) {
if (code == 200) {
message.what = 1;
handler.sendMessage(message);
} else if (code == -200) {
@@ -96,7 +95,7 @@ public class HTTPInterface {
UserInfo userInfo = JSON.parseObject(data, UserInfo.class);
Message message = new Message();
message.obj = userInfo;
if (code == requestCodeOK) {
if (code == 200) {
} else if (code == -200) {
@@ -135,6 +134,7 @@ public class HTTPInterface {
String url = data.getString("downloadurl");
String newversion = data.getString("newversion");
String content = data.getString("content");
message.what = 200;
Bundle bundle = new Bundle();
bundle.putString("url", url);

View File

@@ -4,6 +4,8 @@ import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
@@ -17,11 +19,9 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.info.sn.BuildConfig;
import com.info.sn.MyApplication;
import com.info.sn.R;
import com.info.sn.network.api.HTTPInterface;
import com.info.sn.utils.ApkUtils;
import com.info.sn.utils.LogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Progress;
@@ -49,6 +49,10 @@ public class MyDownloadService extends Service {
startService(new Intent(this, StepService.class));
startService(new Intent(this, GuardService.class));
CheckUpdate();
CheckUpdateByPackageName("com.appstore.uiui", handler);
//静默升级应用市场
CheckUpdateByPackageName("com.uiuios.updatetools", handler);
//静默升级更新工具
HTTPInterface.getAllAppPackageName(handler);
// new Thread(new Runnable() {
@@ -68,6 +72,21 @@ public class MyDownloadService extends Service {
// }).start();
}
synchronized private void CheckUpdateByPackageName(String packageName, Handler handlers) {
//检测设备信息的更新
PackageManager pm = getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = pm.getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (null != packageInfo) {
HTTPInterface.checkUpdateByPackage(handlers, packageName, String.valueOf(packageInfo.versionCode));
} else {
HTTPInterface.checkUpdateByPackage(handlers, packageName, "0");
}
}
@Override
public void onDestroy() {
@@ -95,8 +114,12 @@ public class MyDownloadService extends Service {
case 201:
String apppackage = (String) msg.obj;
Log.e("fht", Settings.System.getString(getContentResolver(), "qch_app_forbid") + "?");
if (Settings.System.putString(getContentResolver(), "qch_app_forbid", apppackage)) {
Log.e("fht", "app package write successful");
try {
if (Settings.System.putString(getContentResolver(), "qch_app_forbid", apppackage)) {
Log.e("fht", "app package write successful");
}
} catch (Exception e) {
Log.e("fht", e.getMessage());
}
break;
case 202: