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

本文介绍了如何在Android应用中实现在线更新功能,详细讲解了如何创建xml布局文件、添加依赖、设置manifest清单,以及使用HttpUtils进行文件下载和安装。同时,文章还涵盖了通知栏更新进度的展示和AndroidX的适配。
摘要由CSDN通过智能技术生成

}

public LoadingService() {

super(“MyService”);

}

public static void startUploadImg(Context context)

{

Intent intent = new Intent(context, LoadingService.class);

context.startService(intent);

}

public void onCreate() {

super.onCreate();

httpUtils = new HttpUtils();

httpUtils.configCurrentHttpCacheExpiry(0);

nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

sharedPreferences = getSharedPreferences(“data”,MODE_PRIVATE);

}

@Override

protected void onHandleIntent(@Nullable Intent intent) {

updateApk();

}

private void updateApk(){

url = sharedPreferences.getString(“url”,“”);

path = sharedPreferences.getString(“path”,“”);

httpUtils.download(url,

path , new RequestCallBack() {

@Override

public void onLoading(final long total, final long current,

boolean isUploading) {

createNotification(total,current);

sendBroadcast(new Intent().setAction(“android.intent.action.loading”));//发送正在下载的广播

super.onLoading(total, current, isUploading);

}

@Override

public void onSuccess(ResponseInfo arg0) {

nm.cancel(R.layout.notification_item);

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

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.i

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值