App更新

之前项目用的友盟更新,但友盟不提供此服务了,于是我们需要自己写了

首先你需要检查更新,检查之后比对版本号:联网获取版本信息我就不写了,获取之后比对版本号,需要更新就弹出更新提示框:

 if (Util.getVersionCode(context) < versionCode.getVersion()) {
            if (context != null) {
            //UpDataActivity是更新弹框,自己写就好
                Intent intent = new Intent(context.getApplicationContext(), UpDataActivity.class);
                intent.putExtra(UpDataActivity.UPDATEOBJ, versionCode);
                context.startActivity(intent);
            }


        } else {
            AppUpdateSaveMsgUtil.clear(context);
//            不需要更新时,将apk文件删除,避免占用不必要的空间
            File updateDir = new File(context.getExternalFilesDir(AppConstant.UPDATEFILENAME).getAbsolutePath());
            OperationFileHelper.deleteDirectory(updateDir.getPath());
            if (isShowNoUpdataToast) {
                Toast.makeText(context, "您现在已经是最新版本", Toast.LENGTH_LONG).show();
            }
        }
然后更新,点击确定就开始更新好了:
  updateNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (Environment.getExternalStorageState().equals(
                        Environment.MEDIA_MOUNTED)) {
                    path = versionCode.getDownload_url();
//                    //自己写的下载逻辑
//                    if (!application.isUpdate()) {
//                        //TODO 这里你可以添加是否已经下载成功的逻辑判断如果该版本已经下载完毕,直接安装,否则启动服务下载
//                        Intent intent = new Intent(getApplicationContext(), UpdateService.class);
//                        intent.putExtra("Key_Down_Url", versionCode.getDownload_url());
//                        startService(intent);
//                    } else {
//                        Toast.makeText(getApplicationContext(), "APP正在下载", Toast.LENGTH_LONG).show();
//                    }
                    // 调用DownloadManager下载
                    Intent intent = new Intent(getApplicationContext(), ApkDownLoadService.class);
                    intent.putExtra(ApkDownLoadService.DownloadUrl, versionCode.getDownload_url());
                    intent.putExtra(ApkDownLoadService.VersionCode, versionCode.getVersion());
                    startService(intent);
                } else {

                    Toast.makeText(getApplication().getApplicationContext(), "SD卡不可用,请插入SD卡",
                            Toast.LENGTH_SHORT).show();
                }
                Log.d("service", "starservice");
                UpDataActivity.this.finish();
            }
        });
 
本文提供两种跟新方式:第一种DownloadManag:
package com.example.likangxin.servicedownload.DownloadManagerDemon;

import android.app.DownloadManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;

import com.example.likangxin.servicedownload.AppConstant;
import com.example.likangxin.servicedownload.DownLoadFileActivity;

import java.io.File;

/**
 * Created by likangxin on 16-8-26.
 */
public class ApkDownLoadService extends Service {
    private String localPath;
    private long mReference;
    private BroadcastReceiver mReceiver;
    private String down_url;
    private String app_name;
    private DownloadManager manager;
    private long versionCode;
    public static final String DownloadUrl = "DownloadUrl";
    public static final String VersionCode = "VersionCode";
    private Context myContext;
    private Handler handler;
    public static final Uri CONTENT_URI = Uri.parse("content://downloads/my_downloads");
    private DownloadChangeObserver downloadObserver;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
        handler = new Handler();
        down_url = intent.getStringExtra(DownloadUrl);
        versionCode = intent.getLongExtra(VersionCode, -1L);
        manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        app_name = down_url.substring(down_url.lastIndexOf("/") + 1);
        myContext = this;
        long downLoadId = AppUpdateSaveMsgUtil.getLong(this, AppConstant.DOWNLOADID);
        if (downLoadId != -1L && versionCode == AppUpdateSaveMsgUtil.getLong(this, AppConstant.DOWNLOADVersion)) {
//            Log.d("ApkDowmnloadService", "queryDownloadStatus");
            queryDownloadStatus(manager);
        } else {
//            Log.d("ApkDowmnloadService", "down_url, app_name");
            downloadRequest(down_url, app_name);
        }
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Intent dialogIntent = new Intent(myContext, DownLoadFileActivity.class);//下载失败的提示框不需要可以不写
                dialogIntent.putExtra(ApkDownLoadService.DownloadUrl, down_url);
                dialogIntent.putExtra(ApkDownLoadService.VersionCode, versionCode);
                dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(dialogIntent);
            }
        };
        return super.onStartCommand(intent, flags, startId);
    }


    // 执行下载
    private final void downloadRequest(String downloadUrl, String mFileName) {
//        Log.d("ApkDowmnloadService", "downloadRequest");
        localPath = getExternalFilesDir(AppConstant.UPDATEFILENAME).getAbsolutePath();
        manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri = Uri.parse(downloadUrl);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        // 设置本地缓存路径
        request.setDestinationUri(Uri.fromFile(new File(localPath, mFileName)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值