Android 更新升级下载 自定义Updates 兼容版

  1. 下载模块

package com.example.updataapk;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.app.Service;

import android.content.Context;

import android.content.Intent;

import android.net.Uri;

import android.os.Handler;

import android.os.IBinder;

import android.os.Message;

import android.util.Log;

import android.widget.RemoteViews;

public class DownloadService extends Service {

// notification 名字

private String notify_name = “apk正在下载…”;

public static final String INTENT_URL = “url”;

private Context mContext = this;

Notification mNotification;

private static final int NOTIFY_ID = 0;

private NotificationManager mNotificationManager;

/* 下载包安装路径 */

private static final String savePath = Function_Utility.getUpgradePath();

private static final String saveFileName = savePath + “demo.apk”;

private String apkUrl;

private int progress;

boolean canceled;

private Thread downLoadThread;

@Override

public IBinder onBind(Intent intent) {

return null;

}

@Override

public void onCreate() {

super.onCreate();

mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);

}

public int onStartCommand(Intent intent, int flags, int startId) {

Log.i(“DownloadService”, “intent=” + intent.toString() + " ; flags= " + flags + " ; startId" + startId);

if (intent.hasExtra(DownloadService.INTENT_URL)) {

apkUrl = (String) intent.getExtras().get(DownloadService.INTENT_URL);

}

progress = 0;

setUpNotification();

new Thread() {

public void run() {

// 开始下载

startDownload();

};

}.start();

return startId;

};

private void startDownload() {

canceled = false;

downloadApk();

}

private Handler mHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

switch (msg.what) {

case 0:

// 下载完毕

// 取消通知

mNotificationManager.cancel(NOTIFY_ID);

installApk();

break;

case 2:

// 这里是用户界面手动取消,所以会经过activity的onDestroy();方法

// 取消通知

mNotificationManager.cancel(NOTIFY_ID);

break;

case 1:

int rate = msg.arg1;

if (rate < 100) {

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;

}

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

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

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

img

img

img

img

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

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

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

文末

初级工程师拿到需求会直接开始做,然后做着做着发现有问题了,要么技术实现不了,要么逻辑有问题。

而高级工程师拿到需求会考虑很多,技术的可行性?对现有业务有没有帮助?对现有技术架构的影响?扩展性如何?等等…之后才会再进行设计编码阶段。

而现在随着跨平台开发,混合式开发,前端开发之类的热门,Android开发者需要学习和掌握的技术也在不断的增加。

通过和一些行业里的朋友交流讨论,以及参考现在大厂面试的要求。我们花了差不多一个月时间整理出了这份Android高级工程师需要掌握的所有知识体系。你可以看下掌握了多少。

混合式开发,微信小程序。都是得学会并且熟练的

这些是Android相关技术的内核,还有Java进阶

高级进阶必备的一些技术。像移动开发架构项目实战等

Android前沿技术;包括了组件化,热升级和热修复,以及各种架构跟框架的详细技术体系

以上即是我们整理的Android高级工程师需要掌握的技术体系了。可能很多朋友觉得很多技术自己都会了,只是一些新的技术不清楚而已。应该没什么太大的问题。

而这恰恰是问题所在!为什么别人高级工程师能年限突破30万,而你只有十几万呢?

就因为你只需补充你自己认为需要的,但并不知道企业需要的。这个就特别容易造成差距。因为你的技术体系并不系统,是零碎的,散乱的。那么你凭什么突破30万年薪呢?

我这些话比较直接,可能会戳到一些人的玻璃心,但是我知道肯定会对一些人起到点醒的效果的。而但凡只要有人因为我的这份高级系统大纲以及这些话找到了方向,并且付出行动去提升自我,为了成功变得更加努力。那么我做的这些就都有了意义。

喜欢的话请帮忙转发点赞一下能让更多有需要的人看到吧。谢谢!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

droid相关技术的内核,还有Java进阶

[外链图片转存中…(img-t7Ftv7wq-1713761410283)]

高级进阶必备的一些技术。像移动开发架构项目实战等

[外链图片转存中…(img-MSR512N4-1713761410284)]

Android前沿技术;包括了组件化,热升级和热修复,以及各种架构跟框架的详细技术体系

[外链图片转存中…(img-i5zXSlsc-1713761410284)]

以上即是我们整理的Android高级工程师需要掌握的技术体系了。可能很多朋友觉得很多技术自己都会了,只是一些新的技术不清楚而已。应该没什么太大的问题。

而这恰恰是问题所在!为什么别人高级工程师能年限突破30万,而你只有十几万呢?

就因为你只需补充你自己认为需要的,但并不知道企业需要的。这个就特别容易造成差距。因为你的技术体系并不系统,是零碎的,散乱的。那么你凭什么突破30万年薪呢?

我这些话比较直接,可能会戳到一些人的玻璃心,但是我知道肯定会对一些人起到点醒的效果的。而但凡只要有人因为我的这份高级系统大纲以及这些话找到了方向,并且付出行动去提升自我,为了成功变得更加努力。那么我做的这些就都有了意义。

喜欢的话请帮忙转发点赞一下能让更多有需要的人看到吧。谢谢!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值