Files
Xuewang365OSNeutral/app/src/main/java/com/xwad/os/service/DownloadService.java
tongtongstudio 3f83d1b136 version:1.0.4
fix:
update:增加下载
2025-12-15 09:36:54 +08:00

245 lines
10 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.xwad.os.service;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.FileProvider;
import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.task.DownloadTask;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.toast.Toaster;
import com.xwad.os.R;
import com.xwad.os.bean.AriaDownloadInfo;
import com.xwad.os.utils.ApkUtils;
import java.io.File;
import java.lang.reflect.Type;
public class DownloadService extends Service {
private static final String TAG = "DownloadService";
public DownloadService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Aria.download(this).register();
mNotificationManagerCompat = NotificationManagerCompat.from(this);
createNotificationChannel();
createDownloadNotificationChannel();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand: " + intent);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
private static final String CHANNEL_ID = "CHANNEL_ID";
private static final String CHANNEL_NAME = "系统通知";
private static final String CHANNEL_DESCRIPTION = "学王365通知";
private static final String CHANNEL_DOWNLOAD_ID = "DOWNLOAD_CHANNEL";
private static final String CHANNEL_DOWNLOAD_NAME = "下载管理";
private static final String CHANNEL_DOWNLOAD_DESCRIPTION = "下载管理通知";
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(CHANNEL_DESCRIPTION);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
private NotificationManagerCompat mNotificationManagerCompat;
private int NotificationID = 7890;
private void sendSimpleNotification() {
Intent intent = new Intent(this, DownloadService.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("关怀系统正在运行")
// .setContentText("测试内容")
.setAutoCancel(false)
.setShowWhen(false)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_MAX);
// notificationId is a unique int for each notification that you must define
// mNotificationManagerCompat.notify(NotificationID, builder.build());
startForeground(NotificationID, builder.build());
}
private void createDownloadNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_DOWNLOAD_ID, CHANNEL_DOWNLOAD_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(CHANNEL_DOWNLOAD_DESCRIPTION);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
private void sendDownloadRunning(AriaDownloadInfo ariaDownloadInfo, int progress) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_DOWNLOAD_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle(ariaDownloadInfo.getAppName())
.setContentText("下载中:" + progress + "%")
.setAutoCancel(true)
.setShowWhen(true)
.setOngoing(false)
.setOnlyAlertOnce(true)
.setProgress(100, progress, false)
.setPriority(NotificationCompat.PRIORITY_HIGH);
// notificationId is a unique int for each notification that you must define
// mNotificationManagerCompat.notify(ariaDownloadInfo.getAppId(), builder.build());
startForeground(ariaDownloadInfo.getAppId(), builder.build());
}
private void sendDownloadComplete(AriaDownloadInfo ariaDownloadInfo, String path) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_DOWNLOAD_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle(ariaDownloadInfo.getAppName())
.setContentText("下载完成")
.setAutoCancel(true)
.setShowWhen(true)
.setOngoing(false)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(createIntent(path));
// notificationId is a unique int for each notification that you must define
mNotificationManagerCompat.notify(ariaDownloadInfo.getAppId(), builder.build());
}
/**
* 设置通知点击事件
*
* @return 点击事件
*/
private PendingIntent createIntent(String path) {
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//注意第二个参数要保持和manifest中android:authorities的值相同
Uri uri = FileProvider.getUriForFile(DownloadService.this,
getPackageName() + ".FileProvider", file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
PendingIntent pendingIntent = PendingIntent.getActivity(DownloadService.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
private void sendDownloadFail(AriaDownloadInfo ariaDownloadInfo) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_DOWNLOAD_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle(ariaDownloadInfo.getAppName())
.setContentText("下载失败")
.setAutoCancel(true)
.setShowWhen(true)
.setOngoing(false)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_HIGH);
// notificationId is a unique int for each notification that you must define
mNotificationManagerCompat.notify(ariaDownloadInfo.getAppId(), builder.build());
}
@Download.onTaskRunning
void running(DownloadTask task) {
String jsonString = task.getExtendField();
Log.e(TAG, "running: " + "正在下载:" + task.getPercent() + "% " + jsonString);
AriaDownloadInfo ariaDownloadInfo = getAriaDownloadInfo(jsonString);
if (ariaDownloadInfo != null) {
Toaster.show("正在下载: " + ariaDownloadInfo.getAppName() + "\t" + task.getPercent() + "%");
sendDownloadRunning(ariaDownloadInfo, task.getPercent());
}
}
@Download.onTaskComplete
void taskComplete(DownloadTask task) {
Log.e(TAG, "taskComplete: " + task.getFilePath());
ApkUtils.installApkFile(DownloadService.this, task.getFilePath());
String jsonString = task.getExtendField();
Log.e(TAG, "taskComplete: " + "下载完成:" + jsonString);
AriaDownloadInfo ariaDownloadInfo = getAriaDownloadInfo(jsonString);
if (ariaDownloadInfo != null) {
Toaster.show("下载完成: " + "\t" + ariaDownloadInfo.getAppName());
sendDownloadComplete(ariaDownloadInfo, task.getFilePath());
}
}
@Download.onTaskFail
void taskFail(DownloadTask task, Exception e) {
Log.e(TAG, "taskFail: ");
String jsonString = task.getExtendField();
Log.e(TAG, "taskFail: " + "下载失败:" + jsonString);
AriaDownloadInfo ariaDownloadInfo = getAriaDownloadInfo(jsonString);
if (ariaDownloadInfo != null) {
Toaster.show("下载失败: " + "\t" + ariaDownloadInfo.getAppName());
sendDownloadFail(ariaDownloadInfo);
}
}
private AriaDownloadInfo getAriaDownloadInfo(String jsonString) {
if (!TextUtils.isEmpty(jsonString)) {
Gson gson = new Gson();
Type type = new TypeToken<AriaDownloadInfo>() {
}.getType();
AriaDownloadInfo ariaDownloadInfo = null;
try {
ariaDownloadInfo = gson.fromJson(jsonString, type);
} catch (Exception e) {
Log.e(TAG, "getAriaDownloadInfo: " + e.getMessage());
}
return ariaDownloadInfo;
} else {
return null;
}
}
}