package com.hdl.photovoltaic.widget.apkwgtupload;
|
|
import android.app.Notification;
|
import android.app.NotificationChannel;
|
import android.app.NotificationManager;
|
import android.app.Service;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.os.Build;
|
import android.os.IBinder;
|
|
import androidx.core.app.NotificationCompat;
|
|
import org.jetbrains.annotations.Nullable;
|
|
/**
|
* Created by Zoro
|
* Created on 2021/5/25
|
* description:
|
*/
|
public class ApkDownLoadService extends Service {
|
|
|
private Context context;
|
|
/**
|
* apk更新管理类
|
*/
|
private AppDownloadManager manager;
|
|
/**
|
* 更新Url
|
*/
|
private String updateUrl;
|
|
/**
|
* 下载文件名
|
*/
|
private String downLoadName;
|
|
/**
|
* 下载文件类型 1:wgt 0:apk
|
*/
|
private String downLoadType;
|
|
@Override
|
public void onCreate() {
|
super.onCreate();
|
context = this;
|
manager = new AppDownloadManager(context);
|
startForeground(System.identityHashCode(this), getNotification());
|
if (null != manager) {
|
manager.resume();
|
}
|
}
|
|
private Notification getNotification() {
|
final String CHANNEL_ID = "default";
|
final String CHANNEL_NAME = "Default Channel";
|
NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(NOTIFICATION_SERVICE);
|
|
//适配安卓8.0的消息渠道
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (notificationManager != null) {
|
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
|
notificationManager.createNotificationChannel(channel);
|
}
|
}
|
|
NotificationCompat.Builder notification =
|
new NotificationCompat.Builder(getBaseContext(), CHANNEL_ID);
|
|
notification.setAutoCancel(true)
|
.setDefaults(Notification.DEFAULT_ALL);
|
|
return notification.build();
|
}
|
|
@Override
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
// App.getApp().setShowUpdate(true);
|
updateUrl = intent.getStringExtra("updateUrl");
|
downLoadName = intent.getStringExtra("downLoadName");
|
downLoadType = intent.getStringExtra("downLoadType");
|
if (null != updateUrl) {
|
if (null != manager) {
|
manager.downloadApk(updateUrl, downLoadName, "update...", downLoadName, downLoadType);
|
}
|
}
|
return super.onStartCommand(intent, flags, startId);
|
}
|
|
@Override
|
public void onDestroy() {
|
super.onDestroy();
|
if (null != manager) {
|
manager.onPause();
|
}
|
}
|
|
@Nullable
|
@Override
|
public IBinder onBind(Intent intent) {
|
return null;
|
}
|
}
|