安卓APP升级代码

20160106日补充如何下载完成后自动弹出安卓界面


思路:接收下载完成的广播,根据下载任务的ID获取APK的安装路径弹出安装界面


  <receiver android:name=".receiver.DownLoadCompleteReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            </intent-filter>
        </receiver>

public class DownLoadCompleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            long id = PreferencesUtil.getDownLoadId();
            if (id == 0l) {
                return;
            }
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(id);
            String saveFileName = null;
            Cursor cursor = FileUtils.getDownloadManager(context).query(query);
            if (cursor.moveToFirst()) {
                saveFileName = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
            }
            cursor.close();
            PreferencesUtil.clearDownLoadId();
            /** 下载完成后自动安装apk */
            if (saveFileName != null) {
                Intent intent1 = new Intent(Intent.ACTION_VIEW);
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent1.setDataAndType(Uri.parse(saveFileName), "application/vnd.android.package-archive");
                context.startActivity(intent1);
            }

        }
    }
}



APP升级代码


import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;



public class CheckUpdateUtil implements UICallback {
    Activity mActivity;
    /**
     * 检查更新
     */
    public   void checkUpdate(Activity activity) {
        mActivity = activity;
        NetWork.getInstance(activity).reqLoginVolley(activity, UrlUtil.URL_VERSION_INFO, VersionInfo.class, this, null);
    }

    @Override
    public void netBack(Object ob) {

        try {
            final VersionInfo versionInfo = (VersionInfo)ob;
            if (VersionUtil.getVersionCode(mActivity) < versionInfo.getVersionCode()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                builder.setTitle("检查更新");
                builder.setMessage(String.format("检测到新版(v%s),立即更新\\n%s", versionInfo.getVersionName(),versionInfo.getDes()));
                if (versionInfo.getIsForce() == 1) {
                    builder.setCancelable(false);
                } else {
                    builder.setNegativeButton("稍后再说", null);
                }
                builder.setPositiveButton("立即更新",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int which) {
                                dialog.dismiss();
                                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(versionInfo.getLink()));
                                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                request.setTitle("***);
                                request.setAllowedOverRoaming(false);
                                //设置文件存放目录
                                try {
                                    request.setDestinationInExternalFilesDir(mActivity, Environment.DIRECTORY_DOWNLOADS, "kidswant.apk");
                                     long id = FileUtils.getDownloadManager(mActivity).enqueue(request);
                                     PreferencesUtil.setDownLoadId(id);

 } catch (Exception e) {
可能因为没有外部存储卡或者DownLoadManager被禁用,上面的报异常,所以使用浏览器访问,为了避免个别浏览器检查长网址安全限制下载文件的原因,这里后台传过来的地址是新浪t.cn短网址
在线生成地址:http://dwz.aidmin.cn/,有利于微博里面写,避免长度限制
 Uri uri = Uri.parse(versionInfo.getLink());
                                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                    mActivity.startActivity(intent);
                                }
                            }
                        });
                builder.show();
            } else {

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值