Files
FLYSN/app/src/main/java/com/info/sn/service/MyDownloadService.java

172 lines
7.0 KiB
Java

package com.info.sn.service;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;
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;
import com.lzy.okgo.model.Response;
import java.io.File;
// 下载管理服务
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();
HTTPInterface.getAllAppPackageName(handler);
// new Thread(new Runnable() {
// @Override
// public void run() {
// while (true) {
// LogUtils.e("packagename", MyApplication.getTaskPackname());
// LogUtils.e("packagename", String.valueOf(MyApplication.getSystemBattery()));
//// MyApplication.sendStartTime();
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
// }).start();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
break;
case 200:
Bundle bundle = (Bundle) msg.obj;
getFile(bundle);
break;
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");
}
break;
case 202:
break;
}
}
};
private void getFile(final Bundle bundle) {
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");
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.installApk(MyDownloadService.this, file);
dialogInterface.dismiss();
}
});
AlertDialog ad = builder.create();
ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
ad.show();
} else {
OkGo.<File>get(bundle.getString("url"))
.execute(new FileCallback("Sninfo/Update" + bundle.getString("versionCode") + ".apk") {
@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.installApk(MyDownloadService.this, response.body());
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);
}
});
}
}
}