update:2019.03.18

fix:增加桌面,应用市场,updatetools静默安装。增加亮屏后检测更新
add:
This commit is contained in:
2020-03-18 18:40:06 +08:00
parent e898f83670
commit f51ceac69d
8 changed files with 385 additions and 30 deletions

View File

@@ -94,6 +94,7 @@ public class MainActivity extends AppCompatActivity {
initView();
initData();
HTTPInterface.checkDevicesInfo(handler);
HTTPInterface.checkUpdateByself(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE));
startService(new Intent(MainActivity.this, MyDownloadService.class));
}
@@ -135,7 +136,7 @@ public class MainActivity extends AppCompatActivity {
if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) {
mHits = new long[COUNTS];//重新初始化数组
Toast.makeText(this, "正在检查更新", Toast.LENGTH_SHORT).show();
HTTPInterface.checkUpdateByPackage(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE));
HTTPInterface.checkUpdateByself(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE));
}
}
@@ -275,21 +276,27 @@ public class MainActivity extends AppCompatActivity {
SPUtils.put(MainActivity.this, "isLogined", 2);
setImageAndText(imageView, "未经验证的设备,请联系客服");
break;
case 200:
case 300:
Bundle bundle = (Bundle) msg.obj;
getFile(bundle);
break;
case -200:
ToastUtil.show("已是最新版本");
break;
case -300:
ToastUtil.show("网络连接失败,检查网络连接");
break;
}
}
};
private void getFile(final Bundle bundle) {
String url = bundle.getString("url");
final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/");
path.mkdirs();
final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/Update" + bundle.getString("versionCode") + ".apk");
final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/" + url.substring(url.lastIndexOf("/") + 1));
if (file.exists() && file.isFile()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
@@ -309,8 +316,8 @@ public class MainActivity extends AppCompatActivity {
ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
ad.show();
} else {
OkGo.<File>get(bundle.getString("url"))
.execute(new FileCallback("Sninfo/Update" + bundle.getString("versionCode") + ".apk") {
OkGo.<File>get(url)
.execute(new FileCallback("Sninfo/" + url.substring(url.lastIndexOf("/") + 1)) {
@Override
public void onSuccess(final Response<File> response) {
// Settings.System.putString(getApplicationContext().getContentResolver(), "qch_app_forbid", "com.baidu.video");

View File

@@ -143,20 +143,65 @@ public class HTTPInterface {
message.obj = bundle;
}
handler.sendMessage(message);
Log.e("checkUpdateByPackage", msg);
Log.e("checkUpdateSN", msg);
} else {
handler.sendEmptyMessage(-200);
Log.e("checkUpdateByPackage", msg);
Log.e("checkUpdateSN", msg);
}
} catch (Exception e) {
Log.e("checkUpdateByPackage", e.getMessage());
Log.e("checkUpdateSN", e.getMessage());
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
Log.e("checkUpdateByPackage", response.getException().toString());
handler.sendEmptyMessage(-300);
Log.e("checkUpdateSN", response.getException().toString());
}
});
}
synchronized public static void checkUpdateByself(final Handler handler, String packageName, String versionCode) {
OkGo.<String>post(UrlPath.GET_APP_UPDATE)
.params("code", versionCode)
.params("package", packageName)
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
try {
JSONObject body = JSON.parseObject(response.body());
int code = body.getInteger("code");
String msg = body.getString("msg");
if (code == 200) {
Message message = new Message();
JSONObject data = JSON.parseObject(body.getString("data"));
if (data != null) {
String url = data.getString("downloadurl");
String newversion = data.getString("newversion");
String content = data.getString("content");
message.what = 300;
Bundle bundle = new Bundle();
bundle.putString("url", url);
bundle.putString("versionCode", newversion);
bundle.putString("content", content);
message.obj = bundle;
}
handler.sendMessage(message);
Log.e("checkUpdateSN", msg);
} else {
handler.sendEmptyMessage(-200);
Log.e("checkUpdateSN", msg);
}
} catch (Exception e) {
Log.e("checkUpdateSN", e.getMessage());
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
handler.sendEmptyMessage(-300);
Log.e("checkUpdateSN", response.getException().toString());
}
});
}

View File

@@ -11,12 +11,24 @@ public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BootReceiver", intent.getAction());
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Log.e("SNBootReceiver", intent.getAction());
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")
|| intent.getAction().equals("android.intent.action.BATTERY_CHANGED")
|| intent.getAction().equals("android.intent.action.BATTERY_LEVEL_CHANGED")
|| intent.getAction().equals("android.intent.action.BATTERY_LOW")
|| intent.getAction().equals("android.intent.action.BATTERY_OKAY")
|| intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED")
|| intent.getAction().equals("android.intent.action.ACTION_POWER_DISCONNECTED")
|| intent.getAction().equals("android.intent.action.DATE_CHANGED")
|| intent.getAction().equals("android.intent.action.TIME_TICK")
|| intent.getAction().equals("android.intent.action.USER_PRESENT")
|| intent.getAction().equals("android.intent.action.ACTION_SCREEN_ON")
|| intent.getAction().equals("android.intent.action.ACTION_SCREEN_OFF")) {
// Intent i = new Intent(context, InitJpushServer.class);
// context.startService(i);
context.startService(new Intent(context, StepService.class));
context.startService(new Intent(context, GuardService.class));
context.startService(new Intent(context, MyDownloadService.class));
}
}

View File

@@ -36,24 +36,24 @@ public class MyDownloadService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
flags = START_STICKY;
return super.onStartCommand(intent, flags, startId);
}
synchronized private void CheckUpdate() {
HTTPInterface.checkUpdateByPackage(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE));
}
@Override
public void onCreate() {
super.onCreate();
startService(new Intent(this, StepService.class));
startService(new Intent(this, GuardService.class));
CheckUpdate();
CheckUpdateByPackageName("com.appstore.uiui", handler);
//静默升级应用市场
CheckUpdateByPackageName("com.android.uiuios", handler);
//静默升级桌面
CheckUpdateByPackageName("com.uiuios.updatetools", handler);
//静默升级更新工具
HTTPInterface.getAllAppPackageName(handler);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
// new Thread(new Runnable() {
// @Override
@@ -72,8 +72,11 @@ public class MyDownloadService extends Service {
// }).start();
}
synchronized private void CheckUpdate() {
HTTPInterface.checkUpdateByself(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE));
}
synchronized private void CheckUpdateByPackageName(String packageName, Handler handlers) {
//检测设备信息的更新
PackageManager pm = getPackageManager();
PackageInfo packageInfo = null;
try {
@@ -124,14 +127,85 @@ public class MyDownloadService extends Service {
break;
case 202:
break;
case 300:
Bundle b = (Bundle) msg.obj;
update(b);
break;
}
}
};
private void getFile(final Bundle bundle) {
String url = bundle.getString("url");
final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/");
path.mkdirs();
final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/Update" + bundle.getString("versionCode") + ".apk");
final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/" + url.substring(url.lastIndexOf("/") + 1));
if (file.exists() && file.isFile()) {
// AlertDialog.Builder builder = new AlertDialog.Builder(this)
// .setTitle("软件更新")
// .setIcon(R.mipmap.ic_launcher)
// .setCancelable(false)
// .setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
// .setPositiveButton("确定", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
ApkUtils.installApp(MyDownloadService.this, file.getAbsolutePath());
// dialogInterface.dismiss();
// }
// });
// AlertDialog ad = builder.create();
// ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
// ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
// ad.show();
} else {
OkGo.<File>get(url)
.execute(new FileCallback("Sninfo/" + url.substring(url.lastIndexOf("/") + 1)) {
@Override
public void onSuccess(final Response<File> response) {
// Settings.System.putString(getApplicationContext().getContentResolver(), "qch_app_forbid", "com.baidu.video");
// ApkUtils.installApkInSilence(response.body().getAbsolutePath(), Launcher.this.getPackageName());
// AlertDialog.Builder builder = new AlertDialog.Builder(MyDownloadService.this)
// .setTitle("软件更新")
// .setIcon(R.mipmap.ic_launcher)
// .setCancelable(false)
// .setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
// .setPositiveButton("确定", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
ApkUtils.installApp(MyDownloadService.this, response.body().getAbsolutePath());
// dialogInterface.dismiss();
// }
// });
// AlertDialog ad = builder.create();
// ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
// ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
// ad.show();
Log.e("getFile", "download file successful,now installing");
}
@Override
public void onError(Response<File> response) {
super.onError(response);
Log.e("getFile", "File download Failure" + response.getException());
}
@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
Log.e("getFile", "已下载:" + progress.currentSize + ",总大小:" + progress.totalSize + ",进度:" + progress.fraction + ",当前网速:" + progress.speed);
}
});
}
}
private void update(final Bundle bundle) {
String url = bundle.getString("url");
final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/");
path.mkdirs();
final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/" + url.substring(url.lastIndexOf("/") + 1));
if (file.exists() && file.isFile()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
@@ -151,8 +225,8 @@ public class MyDownloadService extends Service {
ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
ad.show();
} else {
OkGo.<File>get(bundle.getString("url"))
.execute(new FileCallback("Sninfo/Update" + bundle.getString("versionCode") + ".apk") {
OkGo.<File>get(url)
.execute(new FileCallback("Sninfo/" + url.substring(url.lastIndexOf("/") + 1)) {
@Override
public void onSuccess(final Response<File> response) {
// Settings.System.putString(getApplicationContext().getContentResolver(), "qch_app_forbid", "com.baidu.video");

View File

@@ -16,6 +16,7 @@ import android.widget.Toast;
import androidx.core.content.FileProvider;
import com.info.sn.BuildConfig;
import com.info.sn.R;
import java.io.BufferedReader;
@@ -335,6 +336,49 @@ public class ApkUtils {
e.printStackTrace();
}
}
public static boolean installApp(Context context, String apkPath) {
ToastUtil.show("正在安装应用...");
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = new StringBuilder();
StringBuilder errorMsg = new StringBuilder();
try {
process = new ProcessBuilder("pm", "install", "-i", BuildConfig.APPLICATION_ID, "--user", "0", apkPath).start();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
} catch (Exception e) {
} finally {
try {
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (Exception e) {
}
if (process != null) {
process.destroy();
}
}
Log.e("result", "" + errorMsg.toString());
//如果含有“success”认为安装成功
Log.e("installApp", successMsg.toString());
// if (!successMsg.toString().equalsIgnoreCase("success")) {
// ApkUtils.install(context, new File(apkPath));
// }
return successMsg.toString().equalsIgnoreCase("success");
}
public static void deleteApkInSilence(String packageName) {
Class<?> pmService;