Android 更新升级下载 自定义Updates 兼容版,GitHub上标星13k的《Android面试突击版》

RemoteViews contentview = mNotification.contentView;

contentview.setTextViewText(R.id.tv_progress, rate + “%”);

contentview.setProgressBar(R.id.progressbar, 100, rate, false);

} else {

// 下载完毕后变换通知形式

mNotification.flags = Notification.FLAG_AUTO_CANCEL;

mNotification.contentView = null;

mNotification.setLatestEventInfo(mContext, “下载完成”, “文件已下载完毕”, null);

stopSelf();// 停掉服务自身

}

PendingIntent contentIntent2 = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);

mNotification.contentIntent = contentIntent2;

mNotificationManager.notify(NOTIFY_ID, mNotification);

break;

case 3:

mNotification.flags = Notification.FLAG_AUTO_CANCEL;

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

contentView.setTextViewText(R.id.name, “下载失败”);

// 指定个性化视图

mNotification.contentView = contentView;

Intent intent = new Intent(getApplicationContext(), MainActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// 指定内容意图

mNotification.contentIntent = contentIntent;

mNotificationManager.notify(NOTIFY_ID, mNotification);

stopSelf();// 停掉服务自身

break;

}

}

};

/**

  • 安装apk

  • @param url

*/

private void installApk() {

File apkfile = new File(saveFileName);

if (!apkfile.exists()) {

return;

}

Intent i = new Intent(Intent.ACTION_VIEW);

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

i.setDataAndType(Uri.parse(“file://” + apkfile.toString()), “application/vnd.android.package-archive”);

mContext.startActivity(i);

}

private int lastRate = 0;

private InputStream is = null;

private FileOutputStream fos = null;

/**

  • 下载apk

  • @param url

*/

private void downloadApk() {

downLoadThread = new Thread(mdownApkRunnable);

downLoadThread.start();

}

private Runnable mdownApkRunnable = new Runnable() {

@Override

public void run() {

try {

URL url = new URL(apkUrl);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.connect();

int length = conn.getContentLength();

is = conn.getInputStream();

File file = new File(savePath);

if (!file.exists()) {

file.mkdirs();

}

String apkFile = saveFileName;

File ApkFile = new File(apkFile);

fos = new FileOutputStream(ApkFile);

int count = 0;

byte buf[] = new byte[1024];

do {

int numread = is.read(buf);

count += numread;

progress = (int) (((float) count / length) * 100);

// 更新进度

Message msg = mHandler.obtainMessage();

msg.what = 1;

msg.arg1 = progress;

if (progress >= lastRate + 1) {

mHandler.sendMessage(msg);

lastRate = progress;

}

if (numread <= 0) {

mHandler.sendEmptyMessage(0);// 下载完成通知安装

// 下载完了,cancelled也要设置

canceled = true;

break;

}

fos.write(buf, 0, numread);

} while (!canceled);// 点击取消就停止下载.

Log.i(“DownloadService----------canceled”, canceled + “”);

fos.close();

is.close();

} catch (Exception e) {

Message msg = mHandler.obtainMessage();

msg.what = 3;

mHandler.sendMessage(msg);

e.printStackTrace();

} finally {

try {

if (fos != null) {

fos.close();

}

is.close();

if (is != null) {

is.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

};

/**

  • 创建通知

*/

private void setUpNotification() {

int icon = R.drawable.ic_launcher;

CharSequence tickerText = “开始下载”;

long when = System.currentTimeMillis();

mNotification = new Notification(icon, tickerText, when);

;

// 放置在"正在运行"栏目中

mNotification.flags = Notification.FLAG_ONGOING_EVENT;

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

contentView.setTextViewText(R.id.name, notify_name);

// 指定个性化视图

mNotification.contentView = contentView;

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);

// 指定内容意图

mNotification.contentIntent = contentIntent;

mNotificationManager.notify(NOTIFY_ID, mNotification);

}

}

  1. 下载到手机的地址

package com.example.updataapk;

import java.io.File;

import java.io.IOException;

import android.annotation.SuppressLint;

import android.content.Context;

import android.os.Environment;

@SuppressWarnings(“deprecation”)

@SuppressLint({ “DefaultLocale”, “SimpleDateFormat” })

public class Function_Utility {

private static Context mAppContext;

public static void setAppContext(Context context) {

mAppContext = context;

}

public static Context getAppContext() {

return mAppContext;

}

/**

  • 下载到SD卡地址

*/

public static String getUpgradePath() {

String filePath = getAppRootPath() + “/upgrade/”;

File file = new File(filePath);

if (!file.isDirectory()) {

file.mkdirs();

}

file = null;

return filePath;

}

public static String getAppRootPath() {

String filePath = “/weimicommunity”;

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

filePath = Environment.getExternalStorageDirectory() + filePath;

} else {

filePath = getAppContext().getCacheDir() + filePath;

}

File file = new File(filePath);

if (!file.exists()) {

file.mkdirs();

}

file = null;

File nomedia = new File(filePath + “/.nomedia”);

if (!nomedia.exists())

try {

nomedia.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

return filePath;

}

}

  1. Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android=“http://schemas.android.com/apk/res/android”

package=“com.example.updataapk”

android:versionCode=“1”

android:versionName=“1.0” >

<uses-sdk

android:minSdkVersion=“8”

android:targetSdkVersion=“17” />

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

总结

其实要轻松掌握很简单,要点就两个:

  1. 找到一套好的视频资料,紧跟大牛梳理好的知识框架进行学习。
  2. 多练。 (视频优势是互动感强,容易集中注意力)

你不需要是天才,也不需要具备强悍的天赋,只要做到这两点,短期内成功的概率是非常高的。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。下面资料部分截图是我花费几个月时间整理的,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。

Android开发知识点,真正体系化!**

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-sGzQ4eFt-1711624481753)]

总结

其实要轻松掌握很简单,要点就两个:

  1. 找到一套好的视频资料,紧跟大牛梳理好的知识框架进行学习。
  2. 多练。 (视频优势是互动感强,容易集中注意力)

你不需要是天才,也不需要具备强悍的天赋,只要做到这两点,短期内成功的概率是非常高的。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。下面资料部分截图是我花费几个月时间整理的,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。

[外链图片转存中…(img-227fgYrj-1711624481753)]

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值