android apk更新

一,检测是否需要更新

1-1,检查当前的网络状态

1-2,获取当前版本信息

1-3,检测是否具有新版本,用户选择是否跟新

二,下载最新apk

2-1,下载最新版本的apk

三,启动更新

3-1,启动更新


1-1,检查当前的网络状态:

    /**
     * 获取网络类型
     *
     * @param context Context
     * @return 网络类型
     * @see [类、类#方法、类#成员]
     */
    public static int getNetworkType(Context context) {

        NetworkInfo networkInfo = getConnectedNetworkInfo(context);
        if (networkInfo != null) {
            return networkInfo.getType();
        }

        return -1;
    }

    public static NetworkInfo getConnectedNetworkInfo(Context context) {
        try {
            ConnectivityManager connectivity = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity == null) {
                Log.i(tag, "couldn't get connectivity manager");
            } else {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
                if (info != null) {
                    for (int i = 0; i < info.length; i++) {
                        if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                            return info[i];
                        }
                    }
                }
            }

        } catch (Exception e) {
            Log.i(tag, e.getMessage());
        }
        return null;
    }

1-2,获取当前版本信息

    public void check(View view){
        PackageManager manager = getPackageManager();
        PackageInfo info = null;
        try {
             info = manager.getPackageInfo("com.wang.demo_android", 0);
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        tv_show.setText("versionCode:"+ info.versionCode +"; versionName:"+ info.versionName);
    }

1-3,检测是否具有新版本,用户选择是否跟新(一般的请求网络,弹窗就好)



2-1,下载最新版本的apk,使用android 系统自带的DownloadManager

        mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

        // 设置下载的网络地址:url
        Uri uri = Uri
                .parse("http://gdown.baidu.com/data/wisegame/fd84b7f6746f0b18/baiduyinyue_4802.apk");
        DownloadManager.Request down = new DownloadManager.Request(uri);

        // 设置允许下载的网络类型
        down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
                | DownloadManager.Request.NETWORK_WIFI);

        // 下载途中通知栏的显示
        down.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        // 下载界面的显示
        down.setVisibleInDownloadsUi(true);
        // 设置下载后文件的存放路径
        down.setDestinationInExternalFilesDir(getApplicationContext(),
                Environment.DIRECTORY_DOWNLOADS, "baidumusic.apk");

        // 添加到任务队列
        mDownloadManager.enqueue(down);

3-1,下载完成之后(使用广播接收器),启动更新

广播接收:

    class DownloadUpdateCompleteReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //安装apk
            if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())){
                Log.i(tag, "onReceive");
                //获得下载文件的Id
                long apkId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                Uri apkUri = mDownloadManager.getUriForDownloadedFile(apkId);
                //自动安装apk
                installApk(apkUri);
                UpdateService.this.stopSelf();
            }
        }

    }

注册:

registerReceiver(mDownloadUpdateCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

启动安装:

    private void installApk(Uri apkUri) {
        //通过Intent安装apk
        Log.i(tag, "uri-2->"+apkUri.getPath());
        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.addCategory(android.content.Intent.CATEGORY_DEFAULT);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            getApplicationContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i(tag, "installApk");
    }

推荐:http://www.cnblogs.com/wainiwann/archive/2012/03/12/2391810.html






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章