安卓8.0之后 原来的Notification 有所变动 已不满足需求
1. 首先增加了 NotificationChannel 属性 需要对其进行相关配置
private static void createNotification(NotificationManager notificationManager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CALENDAR_ID, "ander drowload apk default channel.",
NotificationManager.IMPORTANCE_MIN);
// 设置渠道描述
notificationChannel.setDescription("通知更新");
// 是否绕过请勿打扰模式
notificationChannel.canBypassDnd();
// 设置绕过请勿打扰模式
notificationChannel.setBypassDnd(true);
// 桌面Launcher的消息角标
notificationChannel.canShowBadge();
// 设置显示桌面Launcher的消息角标
notificationChannel.setShowBadge(true);
// 设置通知出现时声音,默认通知是有声音的
notificationChannel.setSound(null, null);
// 设置通知出现时的闪灯(如果 android 设备支持的话)
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.RED);
// 设置通知出现时的震动(如果 android 设备支持的话)
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}
}
2. 原来的Notification 方法已经不适用于8.0 系统
public NotificationCompat.Builder getNotificationBuilder() {
return new NotificationCompat.Builder(getApplicationContext(), CALENDAR_ID)
.setWhen(System.currentTimeMillis())
.setContentTitle("title")
.setContentText("context...")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher_round))
.setAutoCancel(true);
}
3. 下载APK的方法 并实时更新通知栏进度
DownloadUtil.get().download(DownloadConstant.apkDown, DownloadConstant.PATH_APK_DOWNLOAD_MANAGER, "zhaoyu.apk",
new DownloadUtil.OnDownloadListener() {
@Override
public void onDownloadSuccess(File file) {
notificationBuilder .setContentText("下载成功,点击安装应用");
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setAutoCancel(true);
notification = notificationBuilder.build();
notificationManager.cancel(NotificationID);
notificationManager.notify(0, notification);
stopSelf();
}
@Override
public void onDownloading(int progress) {
notificationBuilder .setContentText("当前下载进度 " + progress + "%");
notification = notificationBuilder.build();
notificationManager.notify(NotificationID, notification);
}
@Override
public void onDownloadFailed(Exception e) {
notificationBuilder .setContentText("下载失败");
notificationBuilder.setAutoCancel(true);
notification = notificationBuilder.build();
notificationManager.cancel(NotificationID);
notificationManager.notify(0, notification);
stopSelf();
}
});
我这里用的是Okhttp3 请求的下载请求 方法有很多种 只能可以正常下载即可
public class DownloadUtil {
private static DownloadUtil downloadUtil;
private static OkHttpClient okHttpClient;
public static DownloadUtil get() {
if (downloadUtil == null) {
downloadUtil = new DownloadUtil();
}
return downloadUtil;
}
public DownloadUtil() {
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(500, TimeUnit.MILLISECONDS)
.readTimeout(500, TimeUnit.MILLISECONDS)
.build();
}
public static void download( String url, String destFileDir, String destFileName, OnDownloadListener listener) {
Request request = new Request.Builder()
.url(url)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
listener.onDownloadFailed(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
File dir = new File(destFileDir);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, destFileName);
try {
is = response.body().byteStream();
long total = response.body().contentLength();
fos = new FileOutputStream(file);
long sum = 0;
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
sum += len;
int progress = (int) (sum * 1.0f / total * 100);
listener.onDownloading(progress);
}
if (null != fos){
fos.flush();
listener.onDownloadSuccess(file);
}
} catch (Exception e) {
listener.onDownloadFailed(e);
}finally {
try {
if (is != null) {
is.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
}
}
}
});
}
public interface OnDownloadListener{
/**
* 下载成功之后的文件
*/
void onDownloadSuccess(File file);
/**
* 下载进度
*/
void onDownloading(int progress);
/**
* 下载异常信息
*/
void onDownloadFailed(Exception e);
}
}
这里是下载方法发
4. 下载完成后可根据实际的实际需求 进行安装 另外10.0之后 安装方法也需要有所调整
// 通过Intent安装APK文件
public static void installApk(Context context){
File file = new File(DownloadConstant.PATH_APK_DOWNLOAD_MANAGER+"/zhaoyu.apk");
Intent intent = new Intent("android.intent.action.VIEW");
//适配N
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N){
Uri contentUrl = FileProvider.getUriForFile(context,"com.zhaoyugf.zhaoyu.FileProvider",file);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUrl,"application/vnd.android.package-archive");
}else{
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
context.startActivity(intent);
}
最后别忘了注册service
在AndroidManifest.xml 中的 application节点下进行注册
附上完整代码
package com.zhaoyugf.zhaoyu.service;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import com.zhaoyugf.zhaoyu.R;
import com.zhaoyugf.zhaoyu.base.MainActivity;
import com.zhaoyugf.zhaoyu.update.DownloadConstant;
import com.zhaoyugf.zhaoyu.update.DownloadUtil;
import java.io.File;
/**
* Created by wangjungang
* E-Mail:811832241@qq.com
* on 2020/4/24
*/
public class UpdateService extends Service {
public static String CALENDAR_ID = "channel_01";
public static int NotificationID = 1;
private NotificationManager notificationManager;
private Notification notification;
private NotificationCompat.Builder notificationBuilder;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
//通知栏跳转Intent
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
createNotification(notificationManager);
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("Installapk", true);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder = getNotificationBuilder();
notification = notificationBuilder.build();
notificationManager.notify(NotificationID, notification);
DownloadUtil.get().download(DownloadConstant.apkDown, DownloadConstant.PATH_APK_DOWNLOAD_MANAGER, "zhaoyu.apk",
new DownloadUtil.OnDownloadListener() {
@Override
public void onDownloadSuccess(File file) {
notificationBuilder .setContentText("下载成功,点击安装应用");
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setAutoCancel(true);
notification = notificationBuilder.build();
notificationManager.cancel(NotificationID);
notificationManager.notify(0, notification);
stopSelf();
}
@Override
public void onDownloading(int progress) {
notificationBuilder .setContentText("当前下载进度 " + progress + "%");
notification = notificationBuilder.build();
notificationManager.notify(NotificationID, notification);
}
@Override
public void onDownloadFailed(Exception e) {
notificationBuilder .setContentText("下载失败");
notificationBuilder.setAutoCancel(true);
notification = notificationBuilder.build();
notificationManager.cancel(NotificationID);
notificationManager.notify(0, notification);
stopSelf();
}
});
return super.onStartCommand(intent, 0, 0);
}
public NotificationCompat.Builder getNotificationBuilder() {
return new NotificationCompat.Builder(getApplicationContext(), CALENDAR_ID)
.setWhen(System.currentTimeMillis())
.setContentTitle("title")
.setContentText("context...")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher_round))
.setAutoCancel(true);
}
private static void createNotification(NotificationManager notificationManager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CALENDAR_ID, "ander drowload apk default channel.",
NotificationManager.IMPORTANCE_MIN);
// 设置渠道描述
notificationChannel.setDescription("通知更新");
// 是否绕过请勿打扰模式
notificationChannel.canBypassDnd();
// 设置绕过请勿打扰模式
notificationChannel.setBypassDnd(true);
// 桌面Launcher的消息角标
notificationChannel.canShowBadge();
// 设置显示桌面Launcher的消息角标
notificationChannel.setShowBadge(true);
// 设置通知出现时声音,默认通知是有声音的
notificationChannel.setSound(null, null);
// 设置通知出现时的闪灯(如果 android 设备支持的话)
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.RED);
// 设置通知出现时的震动(如果 android 设备支持的话)
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}
}
}