安卓APP升级代码

来源:互联网 发布:网络诈骗到哪里报案 编辑:程序博客网 时间:2024/04/30 22:11

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 0
原创粉丝点击