170 lines
6.6 KiB
Java
170 lines
6.6 KiB
Java
package com.fuying.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.IBinder;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
import android.view.WindowManager;
|
||
|
||
import androidx.annotation.Nullable;
|
||
|
||
import com.arialyy.annotations.Download;
|
||
import com.arialyy.aria.core.Aria;
|
||
import com.arialyy.aria.core.task.DownloadTask;
|
||
import com.blankj.utilcode.util.ToastUtils;
|
||
import com.fuying.sn.KeepAliveConnection;
|
||
import com.fuying.sn.R;
|
||
import com.fuying.sn.gson.GsonUtils;
|
||
import com.fuying.sn.service.main.MainService;
|
||
import com.fuying.sn.utils.ApkUtils;
|
||
import com.google.gson.JsonObject;
|
||
|
||
import java.io.File;
|
||
|
||
// 下载管理服务
|
||
public class DownloadService extends Service {
|
||
private static final String TAG = "DownloadService";
|
||
|
||
@Override
|
||
public void onCreate() {
|
||
super.onCreate();
|
||
Log.e(TAG, "onCreate: ");
|
||
Aria.download(this).register();
|
||
//恢复所有未完成的下载任务
|
||
Aria.download(this).resumeAllTask();
|
||
}
|
||
|
||
@Override
|
||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||
startService(new Intent(this, StepService.class));
|
||
startService(new Intent(this, GuardService.class));
|
||
startService(new Intent(this, MainService.class));
|
||
startService(new Intent(this, ManagerService.class));
|
||
startService(new Intent(this, ControlPanelService.class));
|
||
return START_STICKY;
|
||
}
|
||
|
||
@Override
|
||
public void onDestroy() {
|
||
super.onDestroy();
|
||
Log.e(TAG, "onDestroy: ");
|
||
Aria.download(this).unRegister();
|
||
}
|
||
|
||
@Nullable
|
||
@Override
|
||
public IBinder onBind(Intent intent) {
|
||
return new KeepAliveConnection.Stub() {
|
||
};
|
||
}
|
||
|
||
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/" + 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(DownloadService.this, file.getAbsolutePath());
|
||
// dialogInterface.dismiss();
|
||
// }
|
||
// });
|
||
// AlertDialog ad = builder.create();
|
||
// ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
|
||
// ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
|
||
// ad.show();
|
||
} else {
|
||
|
||
}
|
||
}
|
||
|
||
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)
|
||
.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(DownloadService.this, file);
|
||
dialogInterface.dismiss();
|
||
}
|
||
});
|
||
AlertDialog ad = builder.create();
|
||
ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
|
||
ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
|
||
ad.show();
|
||
} else {
|
||
|
||
}
|
||
}
|
||
|
||
//在这里处理任务执行中的状态,如进度进度条的刷新
|
||
@Download.onTaskRunning
|
||
void running(DownloadTask task) {
|
||
Log.e("aria running", "正在下载:" + task.getState() + "-" + task.getPercent() + "--" + task.getExtendField());
|
||
String appName = "";
|
||
try {
|
||
String jsonString = task.getExtendField();
|
||
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
|
||
if (!TextUtils.isEmpty(jsonString) && jsonObject != null) {
|
||
appName = jsonObject.get("app_name").getAsString();
|
||
}
|
||
} catch (Exception e) {
|
||
Log.e("running", "running: " + e.getMessage());
|
||
}
|
||
ToastUtils.showShort("正在下载:" + appName + "-" + task.getPercent() + "%");
|
||
}
|
||
|
||
@Download.onTaskComplete
|
||
void taskComplete(DownloadTask task) {
|
||
//在这里处理任务完成的状态
|
||
ApkUtils.installApp(DownloadService.this, task.getFilePath());
|
||
Log.e("taskComplete", task.getExtendField());
|
||
// Aria.download(this).load(task.getDownloadEntity().getId()).cancel();
|
||
}
|
||
|
||
@Download.onTaskFail
|
||
void taskFail(DownloadTask task, Exception e) {
|
||
// Aria.download(this).resumeAllTask();
|
||
final String filepath = task.getFilePath();
|
||
final String packageName = task.getExtendField();
|
||
try {
|
||
Log.e(TAG, "taskFail: " + "Exception: " + e.getMessage());
|
||
} catch (Exception ex) {
|
||
Log.e(TAG, "taskFail: Exception = " + e.getMessage());
|
||
}
|
||
Log.e(TAG, "taskFail: " + packageName + "filepath: " + filepath);
|
||
Aria.download(this).load(task.getDownloadEntity().getId()).cancel(true);
|
||
|
||
// Aria.download(this)
|
||
// .load(task.getDownloadEntity().getRealUrl()) //读取下载地址
|
||
// .setFilePath(task.getFilePath())
|
||
// .ignoreFilePathOccupy()
|
||
// .setExtendField(task.getExtendField())
|
||
// .create(); //启动下载}
|
||
}
|
||
}
|