App升级一般有两种方式: 第一种,是在App内部升级,自己写下载代码,一种是调到第三方浏览器中,让浏览器下载本应用升级(之前文章有讲过,链接地址:https://blog.youkuaiyun.com/wolfking0608/article/details/79619472)
下面重点介绍应用内部升级
升级工具类如下:
public class UpdateService extends Service {
public static final String TAG = "UpdateService";
public static final String ACTION = "me.shenfan.UPDATE_APP";
public static final String STATUS = "status";
public static final String PROGRESS = "progress";
public static boolean DEBUG = true;
//下载大小通知频率
public static final int UPDATE_NUMBER_SIZE = 1;
public static final int DEFAULT_RES_ID = -1;
public static final int UPDATE_PROGRESS_STATUS = 0;
public static final int UPDATE_ERROR_STATUS = -1;
public static final int UPDATE_SUCCESS_STATUS = 1;
//params
private static final String URL = "downloadUrl";
private static final String ICO_RES_ID = "icoResId";
private static final String ICO_SMALL_RES_ID = "icoSmallResId";
private static final String UPDATE_PROGRESS = "updateProgress";
private static final String STORE_DIR = "storeDir";
private static final String DOWNLOAD_NOTIFICATION_FLAG = "downloadNotificationFlag";
private static final String DOWNLOAD_SUCCESS_NOTIFICATION_FLAG = "downloadSuccessNotificationFlag";
private static final String DOWNLOAD_ERROR_NOTIFICATION_FLAG = "downloadErrorNotificationFlag";
private static final String IS_SEND_BROADCAST = "isSendBroadcast";
private String downloadUrl;
private int icoResId; //default app ico
private int icoSmallResId;
private int updateProgress; //update notification progress when it add number
private String storeDir; //default sdcard/Android/package/update
private int downloadNotificationFlag;
private int downloadSuccessNotificationFlag;
private int downloadErrorNotificationFlag;
private boolean isSendBroadcast;
private UpdateProgressListener updateProgressListener;
private LocalBinder localBinder = new LocalBinder();
/**
* Class used for the client Binder.
*/
public class LocalBinder extends Binder {
/**
* set update progress call back
* @param listener
*/
public void setUpdateProgressListener(UpdateProgressListener listener){
UpdateService.this.setUpdateProgressListener(listener);
}
}
private boolean startDownload;//开始下载
private int lastProgressNumber;
private NotificationCompat.Builder builder;
private NotificationManager manager;
private int notifyId;
private String appName;
private LocalBroadcastManager localBroadcastManager;
private Intent localIntent;
private DownloadApk downloadApkTask;
/**
* whether debug
*/
public static void debug(){
DEBUG = true;
}
private static Intent installIntent(String path){
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//特别特别 注意这里的包名,必须和你应用的包名保持一致,不然下载了也会安装不了,程序闪退!!!
Uri contentUri = FileProvider.getUriForFile(BaseApplication.app, "com.wcyq.gangrong.FileProvider", new File(path));
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
}else {
Uri uri = Uri.fromFile(new File(path));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
}
return intent;
}
private static Intent webLauncher(String downloadUrl){
Uri download = Uri.parse(downloadUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, download);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
private static String getSaveFileName(String downloadUrl) {
if (downloadUrl == null || TextUtils.isEmpty(downloadUrl)) {
return "noName.apk";
}
return downloadUrl.substring(downloadUrl.lastIndexOf("/"));
}
private static File getDownloadDir(UpdateService service){
File downloadDir = null;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
if (service.storeDir != null){
downloadDir = new File(Environment.getExternalStorageDirectory(), service.storeDir);
}else {
downloadDir = new File(service.getExternalCacheDir(), "update");
}
} else {
downloadDir = new File(service.getCacheDir(), "update");
}
if (!downloadDir.exists()) {
downloadDir.mkdirs();
}
return downloadDir;
}
@Override
public void onCreate() {
super.onCreate();
appName = getApplicationName();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!startDownload && intent != null){
startDownload = true;
downloadUrl = intent.getStringExtra(URL);
icoResId = intent.getIntExtra(ICO_RES_ID, DEFAULT_RES_ID);
icoSmallResId = intent.getIntExtra(ICO_SMALL_RES_ID, DEFAULT_RES_ID);
storeDir = intent.getStringExtra(STORE_DIR);
updateProgress = intent.getIntExtra(UPDATE_PROGRESS, UPDATE_NUMBER_SIZE);
downloadNotificationFlag = intent.getIntExtra(DOWNLOAD_NOTIFICATION_FLAG, 0);
downloadErrorNotificationFlag = intent.getIntExtra(DOWNLOAD_ERROR_NOTIFICATION_FLAG, 0);
downloadSuccessNotificationFlag = intent.getIntExtra(DOWNLOAD_SUCCESS_NOTIFICATION_FLAG, 0);
isSendBroadcast = intent.getBooleanExtra(IS_SEND_BROADCAST, false);
if (DEBUG){
Log.d(TAG, "downloadUrl: " + downloadUrl);
Log.d(TAG, "icoResId: " + icoResId);
Log.d(TAG, "icoSmallResId: " + icoSmallResId);
Log.d(TAG, "storeDir: " + storeDir);
Log.d(TAG, "updateProgress: " + updateProgress);
Log.d(TAG, "downloadNotificationFlag: " + downloadNotificationFlag);
Log.d(TAG, "downloadErrorNotificationFlag: " + downloadErrorNotificationFlag);
Log.d(TAG, "downloadSuccessNotificationFlag: " + downloadSuccessNotificationFlag);
Log.d(TAG, "isSendBroadcast: " + isSendBroadcast);
}
notifyId = startId;
buildNotification();
buildBroadcast();
downloadApkTask = new DownloadApk(this);
downloadApkTask.execute(downloadUrl);
}
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return localBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return true;
}
public void setUpdateProgressListener(UpdateProgressListener updateProgressListener) {
this.updateProgressListener = updateProgressListener;
}
@Override
public void onDestroy() {
if (downloadApkTask != null){
downloadApkTask.cancel(true);
}
if (updateProgressListener != null){
updateProgressListener = null;
}
localIntent = null;
builder = null;
super.onDestroy();
}
public String getApplicationName() {
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
try {
packageManager = getApplicationContext().getPackageManager();
applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
applicationInfo = null;
}
String applicationName =
(String) packageManager.getApplicationLabel(applicationInfo);
return applicationName;
}
private void buildBroadcast(){
if (!isSendBroadcast){
return;
}
localBroadcastManager = LocalBroadcastManager.getInstance(this);
localIntent = new Intent(ACTION);
}
private void sendLocalBroadcast(int status, int progress){
if (!isSendBroadcast || localIntent == null){
return;
}
localIntent.putExtra(STATUS, status);
localIntent.putExtra(PROGRESS, progress);
localBroadcastManager.sendBroadcast(localIntent);
}
private void buildNotification(){
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(this);
builder.setContentTitle(getString(R.string.update_app_model_prepare, appName))
.setWhen(System.currentTimeMillis())
.setProgress(100, 1, false)
.setSmallIcon(icoSmallResId)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(), icoResId))
.setDefaults(downloadNotificationFlag);
manager.notify(notifyId, builder.build());
}
private void start(){
builder.setContentTitle(appName);
builder.setContentText(getString(R.string.update_app_model_prepare, 1));
manager.notify(notifyId, builder.build());
sendLocalBroadcast(UPDATE_PROGRESS_STATUS, 1);
if (updateProgressListener != null){
updateProgressListener.start();
}
}
/**
*
* @param progress download percent , max 100
*/
private void update(int progress){
if (progress - lastProgressNumber > updateProgress){
lastProgressNumber = progress;
builder.setProgress(100, progress, false);
builder.setContentText(getString(R.string.update_app_model_progress, progress, "%"));
manager.notify(notifyId, builder.build());
sendLocalBroadcast(UPDATE_PROGRESS_STATUS, progress);
if (updateProgressListener != null){
updateProgressListener.update(progress);
}
}
}
private void success(String path) {
builder.setProgress(0, 0, false);
builder.setContentText(getString(R.string.update_app_model_success));
Intent i = installIntent(path);
PendingIntent intent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(intent);
builder.setDefaults(downloadSuccessNotificationFlag);
Notification n = builder.build();
n.contentIntent = intent;
manager.notify(notifyId, n);
sendLocalBroadcast(UPDATE_SUCCESS_STATUS, 100);
if (updateProgressListener != null){
updateProgressListener.success();
}
startActivity(i);
stopSelf();
}
private void error(){
Intent i = webLauncher(downloadUrl);
PendingIntent intent = PendingIntent.getActivity(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentText(getString(R.string.update_app_model_error));
builder.setContentIntent(intent);
builder.setProgress(0, 0, false);
builder.setDefaults(downloadErrorNotificationFlag);
Notification n = builder.build();
n.contentIntent = intent;
manager.notify(notifyId, n);
sendLocalBroadcast(UPDATE_ERROR_STATUS, -1);
if (updateProgressListener != null){
updateProgressListener.error();
}
stopSelf();
}
private static class DownloadApk extends AsyncTask<String, Integer, String> {
private WeakReference<UpdateService> updateServiceWeakReference;
public DownloadApk(UpdateService service){
updateServiceWeakReference = new WeakReference<>(service);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
UpdateService service = updateServiceWeakReference.get();
if (service != null){
service.start();
}
}
@Override
protected String doInBackground(String... params) {
final String downloadUrl = params[0];
final File file = new File(UpdateService.getDownloadDir(updateServiceWeakReference.get()),
UpdateService.getSaveFileName(downloadUrl));
if (DEBUG){
Log.d(TAG, "download url is " + downloadUrl);
Log.d(TAG, "download apk cache at " + file.getAbsolutePath());
}
File dir = file.getParentFile();
if (!dir.exists()){
dir.mkdirs();
}
HttpURLConnection httpConnection = null;
InputStream is = null;
FileOutputStream fos = null;
int updateTotalSize = 0;
java.net.URL url;
try {
url = new URL(downloadUrl);
httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setConnectTimeout(20000);
httpConnection.setReadTimeout(20000);
if (DEBUG){
Log.d(TAG, "download status code: " + httpConnection.getResponseCode());
}
if (httpConnection.getResponseCode() != 200) {
return null;
}
updateTotalSize = httpConnection.getContentLength();
if (file.exists()) {
if (updateTotalSize == file.length()) {
// 下载完成
return file.getAbsolutePath();
} else {
file.delete();
}
}
file.createNewFile();
is = httpConnection.getInputStream();
fos = new FileOutputStream(file, false);
byte buffer[] = new byte[4096];
int readSize = 0;
int currentSize = 0;
while ((readSize = is.read(buffer)) > 0) {
fos.write(buffer, 0, readSize);
currentSize += readSize;
publishProgress((currentSize * 100 / updateTotalSize));
}
// download success
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file.getAbsolutePath();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (DEBUG){
Log.d(TAG, "current progress is " + values[0]);
}
UpdateService service = updateServiceWeakReference.get();
if (service != null){
service.update(values[0]);
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
UpdateService service = updateServiceWeakReference.get();
if (service != null){
if (s != null){
service.success(s);
}else {
service.error();
}
}
}
}
/**
* a builder class helper use UpdateService
*/
public static class Builder{
private String downloadUrl;
private int icoResId = DEFAULT_RES_ID; //default app ico
private int icoSmallResId = DEFAULT_RES_ID;
private int updateProgress = UPDATE_NUMBER_SIZE; //update notification progress when it add number
private String storeDir; //default sdcard/Android/package/update
private int downloadNotificationFlag;
private int downloadSuccessNotificationFlag;
private int downloadErrorNotificationFlag;
private boolean isSendBroadcast;
protected Builder(String downloadUrl){
this.downloadUrl = downloadUrl;
}
public static Builder create(String downloadUrl){
if (downloadUrl == null) {
throw new NullPointerException("downloadUrl == null");
}
return new Builder(downloadUrl);
}
public String getDownloadUrl() {
return downloadUrl;
}
public int getIcoResId() {
return icoResId;
}
public Builder setIcoResId(int icoResId) {
this.icoResId = icoResId;
return this;
}
public int getIcoSmallResId() {
return icoSmallResId;
}
public Builder setIcoSmallResId(int icoSmallResId) {
this.icoSmallResId = icoSmallResId;
return this;
}
public int getUpdateProgress() {
return updateProgress;
}
public Builder setUpdateProgress(int updateProgress) {
if (updateProgress < 1){
throw new IllegalArgumentException("updateProgress < 1");
}
this.updateProgress = updateProgress;
return this;
}
public String getStoreDir() {
return storeDir;
}
public Builder setStoreDir(String storeDir) {//文件夹名 GangGang
this.storeDir = storeDir;
return this;
}
public int getDownloadNotificationFlag() {
return downloadNotificationFlag;
}
public Builder setDownloadNotificationFlag(int downloadNotificationFlag) {
this.downloadNotificationFlag = downloadNotificationFlag;
return this;
}
public int getDownloadSuccessNotificationFlag() {
return downloadSuccessNotificationFlag;
}
public Builder setDownloadSuccessNotificationFlag(int downloadSuccessNotificationFlag) {//标记等于-1
this.downloadSuccessNotificationFlag = downloadSuccessNotificationFlag;
return this;
}
public int getDownloadErrorNotificationFlag() {
return downloadErrorNotificationFlag;
}
public Builder setDownloadErrorNotificationFlag(int downloadErrorNotificationFlag) {//标记-1
this.downloadErrorNotificationFlag = downloadErrorNotificationFlag;
return this;
}
public boolean isSendBroadcast() {
return isSendBroadcast;
}
public Builder setIsSendBroadcast(boolean isSendBroadcast) {
this.isSendBroadcast = isSendBroadcast;
return this;
}
public Builder build(Context context){
if (context == null){
throw new NullPointerException("context == null");
}
Intent intent = new Intent();//Intent { cmp=com.wcyq.gangrong/.utils.UpdateService }
intent.setClass(context, UpdateService.class);
intent.putExtra(URL, downloadUrl);
if (icoResId == DEFAULT_RES_ID){//进来了
icoResId = getIcon(context);
}
if (icoSmallResId == DEFAULT_RES_ID){//进来了
icoSmallResId = icoResId;
}
intent.putExtra(ICO_RES_ID, icoResId);
intent.putExtra(STORE_DIR, storeDir);
intent.putExtra(ICO_SMALL_RES_ID, icoSmallResId);
intent.putExtra(UPDATE_PROGRESS, updateProgress);
intent.putExtra(DOWNLOAD_NOTIFICATION_FLAG, downloadNotificationFlag);
intent.putExtra(DOWNLOAD_SUCCESS_NOTIFICATION_FLAG, downloadSuccessNotificationFlag);
intent.putExtra(DOWNLOAD_ERROR_NOTIFICATION_FLAG, downloadErrorNotificationFlag);
intent.putExtra(IS_SEND_BROADCAST, isSendBroadcast);
context.startService(intent);
return this;
}
private int getIcon(Context context){
final PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo = null;
try {
appInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (appInfo != null){
return appInfo.icon;
}
return 0;
}
}
使用方法:
private void requestAppVersion() {
RequestParams params = new RequestParams();
addRequestHeader(params);
BaseApplication.getInstance().httpRequest.xPostjson(mContext, params, Constant.BASE_HTTP + ContantUrl.getVersionUpDate, new RequestResultJsonCallBack() {
@Override
public void onSucess(String result) {
Logger.e(TAG, "requestAppVersion-------" + result);
NewBaseBean info = Constant.getPerson(result, NewBaseBean.class);
if (info.getCode() == Constant.RETURN_SUCCESS__STATE_CODE) {
UpdataAppBean bean = Constant.getPerson(result, UpdataAppBean.class);
List<UpdataAppBean.DataBean.ListBean> list = bean.getData().getList();
if (list != null && list.size() > 0) {
listBean = list.get(0);
String name = listBean.getName();
if (!name.equals(ContantUrl.SERVER_VERSION_NAME)) {
final String url = listBean.getUrl();
String text = mContext.getResources().getString(R.string.check_upgrade);
UpgradeDialog upgrade = new UpgradeDialog(MainActivity.this, text, new UpgradeDialog.OnClickconfirmListener() {
@Override
public void confirm() {
if (!checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) && !checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_MAIN);
return;
}
UpdateService.Builder.create(url).setStoreDir(ContantUrl.AppFile).setDownloadSuccessNotificationFlag(Notification.DEFAULT_ALL).setDownloadErrorNotificationFlag(Notification.DEFAULT_ALL).build(mContext);
Toast.makeText(mContext, "正在后台下载", Toast.LENGTH_LONG).show();
//通过浏览器去下载APK
// InstallUtils.installAPKWithBrower(mContext, url);
}
});
upgrade.show();
upgrade.setDetail(listBean.getDescription());
}
}
} else {
Logger.e(TAG, info.getMessage());
}
}
@Override
public void onFailure(int errorCode, String errorMsg) {
showErrorLogger(TAG, errorCode, errorMsg);
}
});
}
public boolean checkPermission(@NonNull String permission) {
return ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_MAIN) {//权限走的是这里
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
UpdateService.Builder.create(listBean.getUrl()).setStoreDir(ContantUrl.AppFile).setDownloadSuccessNotificationFlag(Notification.DEFAULT_ALL).setDownloadErrorNotificationFlag(Notification.DEFAULT_ALL).build(mContext);
Toast.makeText(mContext, "正在后台下载", Toast.LENGTH_LONG).show();
//删除apk文件(获取权限之后)
FileUtils.deleteFile(new File(ContantUrl.absolutePath + File.separator + ContantUrl.AppFile + "/GangGang_release-1.0.apk"));
} else {
ToastUtil.show(mContext, "权限被禁止,无法下载文件");
}
}
}为了大家查看方便,没有使用分层处理. UpgradeDialog升级app内容的弹框,很好写,可以自己写一个,工具类可以照抄,但是逻辑代码仅给各位参考.
特别注意:
1. 这里适配了Android6.0以上的权限, 在清单文件中要注册一个内容提供者,否者你的权限申请不下来.
代码如下:rc_file_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths> 清单文件中要注册
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/rc_file_path"/>
</provider>2. 可能你很久没有用服务了,服务也是需要注册的,否者你发现权限成功了,提示正在下载,就是通知栏里面就是没有下载的进度条,你可能怀疑是不是楼上这个教你的家伙,傻逼了? 其实是你自己没有注册服务,自己犯二了.
3. App下载进度条出来了,但是安装到快100%的时候,妈的,程序闪退了 就是安装不了
请你在复制的时候,查看一下,上面的工具类, 有一行我标了红色了 ,你程序的包名一定要填写上去!!!.因为你复制的是我Demo的包名.FileProvider.所以下载的时候发现不一样,能给你安装成功就他妈见鬼了.
4. 你下载App成功了,也进入下载页面了,麻痹的就是安装apk安装不上去,这是什么鬼? 检查了代码也没有错啊,自己发现自己找了半天原因还是没有找出来问题来? 怎么办?算了,我还是告诉你吧.你检查下,你的app和签名和你手机你的app的签名是否一致,两者的包名是否一致,如果不一致,是不会覆盖安装的,这个与你的版本没毛关系.所以你安装不上去
5.如果上面的4点你都做到了,代码也检查了,还是出现问题,那只能说明你人品太差了,自己好好检查和分析原因debug吧! 以上遇到的坑希望能够帮到你,如果能够帮到你,并节约你开发的时间,请给我点个赞,follow我一下,谢谢!

9174





