安卓APP实现在线更新功能,适配Androidx和9

本文介绍了如何在安卓APP中实现在线更新功能,包括创建res/xml文件夹、添加xUtils依赖、配置manifest清单以及处理不同Android版本的安装逻辑。通过Notification展示下载进度,并在下载完成后启动安装界面。此外,还提供了Utils工具类用于获取版本信息和进行版本比较。
摘要由CSDN通过智能技术生成

installApk();//下载成功 打开安装界面

stopSelf();//结束服务

sendBroadcast(new Intent().setAction(“android.intent.action.loading_over”));//发送下载结束的广播

}

@Override

public void onFailure(HttpException arg0, String arg1) {

Toast.makeText(LoadingService.this,“下载失败…”,Toast.LENGTH_SHORT).show();

sendBroadcast(new Intent().setAction(“android.intent.action.loading_over”));//发送下载结束的广播

nm.cancel(R.layout.notification_item);

stopSelf();

}

});

}

/**

  • 安装下载的新版本

*/

protected void installApk() {

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

File file = new File(path);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

// 7.0 以上

try {

Uri apkUri = FileProvider.getUriForFile(this, “com.jw.fileprovider”, file);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

intent.setDataAndType(apkUri, “application/vnd.android.package-archive”);

}catch (Exception e){

}

} else {

// 7.0以下

Uri uri = Uri.fromFile(file);

intent.setDataAndType(uri, “application/vnd.android.package-archive”);

}

this.startActivity(intent);

}

private void createNotification(final long total, final long current){

NotificationCompat.Builder builder = new NotificationCompat.Builder(this,“default”);

builder.setSmallIcon(R.mipmap.ic_launcher)//必须要设置这个属性,否则不显示

;

RemoteViews contentView = new RemoteViews(this.getPackageName(),R.layout.notification_item);

contentView.setProgressBar(R.id.progress, (int)total, (int)current, false);

builder.setOngoing(true);//设置左右滑动不能删除

// Notification notification = builder.build();

// notification.contentView = contentView;

String id = “channel_001”;

String name = “name”;

Notification notification = null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);

nm.createNotificationChannel(mChannel);

notification = new Notification.Builder(LoadingService.this,“default”)

.setChannelId(id)

.setContentTitle(“客户端更新”)

.setContentText(“更新完毕”)

.setSmallIcon(R.mipmap.ic_launcher).build();

notification.contentView = contentView;

} else {

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(LoadingService.this,“default”)

.setContentTitle(“更新了”)

.setContentText(“更新了xxxxxxxxx内容”)

.setSmallIcon(R.mipmap.ic_launcher)

.setOngoing(true)

;//无效

notification = notificationBuilder.build();

notification.contentView = contentView;

}

nm.notify(R.layout.notification_item,notification);//发送通知

}

}

Utils.java

import android.content.Context;

import android.content.pm.PackageInfo;

import android.content.pm.PackageManager;

public class Utils {

/**

  • 获取版本号

  • @throws PackageManager.NameNotFoundException

*/

public static String getVersionName(Context context) throws PackageManager.NameNotFoundException {

// 获取packagemanager的实例

PackageManager packageManager = context.getPackageManager();

// getPackageName()是你当前类的包名,0代表是获取版本信息

PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);</

  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值