/*********下载游戏apk,并安装,代码有删减*******************/ package com.chinagames.ChinaGameHall.UI; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.text.NumberFormat; import android.app.Activity; import android.app.AlertDialog; import android.app.PendingIntent; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.view.KeyEvent; import android.view.Window; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.chinagames.ChinaGameHall.R; import com.chinagames.ChinaGameHall.Common.BaseControl; import com.chinagames.ChinaGameHall.Common.Common; import com.chinagames.ChinaGameHall.Common.Util; /** * android中国游戏大厅画面 游戏下载和安装页面 */ public class GameDownloadAndInstall extends Activity { // 打印标志 private String Tag = "GameDownloadAndInstall"; // 获得组建对象 private TextView tvname; // 显示游戏名的textview对象 private TextView tvprogress; // 游戏下载进度 private ProgressBar pb;// 进度条对象 private int _progress = 0;// 进度数 private String realURL = "";//apk的位置 private String gamename = "";//apk的名称 private String gameverid = "";// 游戏的版本号 public boolean isStop = false;// 是否停止下载 // 下载保存的位置,为sdcard下; private String fileName = "";// 保存的文件名称 private int fileSize = 1;// 文件大小 private File file;// 文件对象的定义 private String apkName;// 安装包的包名 private String result;// 请求下载的结果信息 private boolean isHall = false;// 是否请求的是大厅apk private HttpURLConnection conn = null;// 连接对象 // new 一个下载类 private task task = new task(); // 下载进度; int progress = 0; /** * 创建视图页面 * * @param state Bundle */ public void onCreate(Bundle state) { super.onCreate(state); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.mainface_gamedownloadprogress); Bundle bundle = getIntent().getExtras(); // 接受从三个activity传来的 游戏的id 版本信息,和下载游戏的url realURL = bundle.getString("URL"); gameid = bundle.getString("gameid"); if (gameid.equals("0")) { Util.printLog(Tag, "**is down load hall!"); isHall = true; Util.printLog(Tag, "**rejister mApplicationsReceiver success!"); } gamename = bundle.getString("gamename"); gameverid = bundle.getString("gameverid"); if (realURL == "" || realURL.equals("") || gameid == "") { tvname.setText("信息获取错误!"); return; } inintContent(); task.execute(realURL); } /** * 重新注册监听 */ protected void onResume() { super.onResume(); } /** * 初始化组件 */ public void inintContent() { // 注册监听 tvname = (TextView) findViewById(R.id.MainFace_DownLoadingTextView01); tvprogress = (TextView) findViewById(R.id.MainFace_DownLoadingTextView02); pb = (ProgressBar) findViewById(R.id.MainFace_DownLoadingProgressBar01); } /** * 阻止返回键退出,给予提示 * * @param keyCode * @param event * @return boolean */ public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: createBackDialog(); return false; case KeyEvent.KEYCODE_HOME: return false; } return false; } /** * 确定退出的对话框创建 */ public void createBackDialog() { AlertDialog.Builder alert1 = new AlertDialog.Builder(this); alert1.setTitle("取消下载").setMessage("确认取消下载吗?").setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // 停止下载apk isStop = true; finish(); } }).setNegativeButton("返回", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).create().show(); } /** * 安装刚下载的游戏 */ public void installGame() { if (!isStop) { try { // 安装之前写id号(lable-id)防止此次下载不安装下次安装时丢失id信息; writeGameId(BaseControl.CURRENT_APP_NAME, gameid, BaseControl.settings_id); // 安装 Uri uri = Uri.parse(fileName); Intent startGameIntent = new Intent(Intent.ACTION_VIEW, uri); startGameIntent.setData(uri); startGameIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startGameIntent .setClassName("com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity"); PendingIntent contentIntent = PendingIntent.getActivity( getApplicationContext(), 0, startGameIntent, 0); startActivity(startGameIntent); BaseControl.isAppInstallByMobile = true; finish(); } catch (Exception e) { e.printStackTrace(); Common.getSureDialog(GameDownloadAndInstall.this, "提示", "此文件不能安装!"); } } } /** * 异步下载apk类 * */ private class task extends AsyncTask<String, String, String> { /** * 异步线程入口 */ protected String doInBackground(String... params) { String pre = params[0]; try { if (pre.contains("http://")) { URL sourceUrl; try { sourceUrl = new URL(pre); fileName = sourceUrl.getFile(); fileName = fileName .substring(fileName.lastIndexOf('/') + 1); apkName = fileName; fileName = BaseControl.HALL_APK_PATH + apkName; File savaFile = new File(BaseControl.HALL_APK_PATH); if (!savaFile.isDirectory()) { savaFile.mkdir(); } FileOutputStream fos = new FileOutputStream(fileName); int read = 0; byte[] buffer = new byte[1024]; conn = (HttpURLConnection) sourceUrl.openConnection(); conn.setDoInput(true); conn.connect(); fileSize = conn.getContentLength(); InputStream is = conn.getInputStream(); int progress = 0; do { read = is.read(buffer); if (read > 0) { // 循环读取写游戏数据到sd卡;读取过程中发送进度消息; fos.write(buffer, 0, read); progress += read; publishProgress("" + progress); } } while (read != -1); fos.close(); is.close(); conn.disconnect(); if (progress != fileSize) { tvname.setText("下载结束!"); return "下载结束"; } } catch (Exception e) { e.printStackTrace(); tvname.setText("下载出现异常!请检查SD卡安装是否正常!"); return "异常"; } } else { } } catch (Exception ex) { ex.printStackTrace(); } finally { } return "任务结束"; } /** * 根据异步线程结果发送消息跟新url * * */ protected void onPostExecute(String result) { super.onPostExecute(result); if (result.equals("下载结束")) { Toast.makeText(GameDownloadAndInstall.this, result, Toast.LENGTH_SHORT).show(); } } /** * 最先执行,在UI线程中被系统调用 */ protected void onPreExecute() { super.onPreExecute(); Toast.makeText(GameDownloadAndInstall.this, "开始执行下载任务 ", Toast.LENGTH_SHORT).show(); } /** * 更新界面操作,在收到更新消息后,在UI线程中被系统调用 */ protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); int progress = Integer.parseInt(values[0]); if (progress >= fileSize) { // 调用安装方法 installGame(); } NumberFormat numberFormat = NumberFormat.getInstance(); // 设置精确到小数点后2位 numberFormat.setMaximumFractionDigits(0); String result = numberFormat.format((float) progress / (float) fileSize * 100); tvname.setText(apkName + " 下载中..."); tvprogress.setText(result + "%"); pb.setProgress(Integer.parseInt(result)); } } /** * 程序释放 */ public void onDestroy() { super.onDestroy(); if (task != null) { task.cancel(true); } Util.printLog(Tag, "onDestroy..isAppInstallByMobile:" + BaseControl.isAppInstallByMobile); } }

下载游戏apk,并安装相关推荐

  1. 下载的apk无法安装等问题,文件无法打开

    [DESCRIPTION] 浏览器下载文件时,有时会遇到下载后文件无法打开,比如ogg,3gp等格式,或者下载的apk无法安装等问题.这类问题一般是sever返回的mimetype有问题,导致打开时, ...

  2. 探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法

    前言 相信这样一个问题,大家都不会陌生, "有什么的方法可以使Android的程序APK不用安装,而能够直接启动". 发现最后的结局都是不能实现这个美好的愿望,而腾讯Android ...

  3. 不安装游戏apk直接启动法

    原文地址:http://blog.zhourunsheng.com/2011/09/%E6%8E%A2%E7%A7%98%E8%85%BE%E8%AE%AFandroid%E6%89%8B%E6%9C ...

  4. android apk下载完成后调用安装

    一般应用里下载完apk的话都会自动调取安装apk的方法,在下载完成后直接调用下面这个方法即可调用apk安装功能 public static void install(Activity activity ...

  5. android 模拟器 启动,android开发之启动模拟器并安装游戏apk

    本文不讲环境设置,也不讲程序代码,咱们想讲如何把一个游戏APK文件,在模拟器上跑起来! 首先到网上下几个ANDROID的游戏到本地保存, 然后启动模拟器! 启动模拟器用命令行 CMD-> CD ...

  6. Android7.0下载Apk自动安装

    Android7.0下载Apk自动安装 1. 整体需求 下载APK文件 使用DownloadManager来下载 在应用界面中展示下载进度 安装下载后的APK文件 root模式: 可以自动安装,不需要 ...

  7. e63 安装java_如何从电脑上下载游戏到诺基亚E63手机上?

    如何从电脑上下载游戏到诺基亚E63手机上?以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 如何从电脑上下载游戏到诺基亚E ...

  8. 解决APK下载到Cache目录安装提示“解析安装包失败”的问题

    下载的APK在使用下面代码安装的时候提示解析安装包失败,APK本身没有问题,使用手机助手或者adb install 都可以正常安装. /**      * 安装应用程序      */     pub ...

  9. 安卓笔记之xutil下载apk并安装

    使用第三方框架xutil:下载apk文件并安装. 进入到安装页面 Intent intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(I ...

最新文章

  1. java代码中fastjson生成字符串和解析字符串的方法和javascript文件中字符串和json数组之间的转换方法...
  2. mxonline实战3,编写首页及用户登录页面1
  3. 数据库查询某一列大写转化小写字母表示_基于MySQL数据库下亿级数据的分库分表...
  4. [知识图谱实战篇] 七.HTML+D3实现关系图谱搜索功能
  5. .NET 动态脚本语言Script.NET系列文章汇总 非常精彩的应用举例
  6. 小猴子蓝裤黄袄的局域网聊天
  7. (51)FPGA状态机描述(四段式)
  8. URL请求到Action的映射规则
  9. Java反射机制的简单应用
  10. 笔记本电脑java记事本在哪_如何打开电脑记事本_电脑记事本在哪
  11. windows7系统怎么内录
  12. 腾讯教育 App Flutter 跨端点播组件实践
  13. 文华财经彩波均线主图指标公式(指标公式源码)破解加密
  14. 固定资产管理系统项目总结
  15. java鼠标点击按钮事件_Java学习——GUI编程(鼠标单击按钮事件)
  16. React 全家桶(react脚手架 redux react-redux react-router-dom ui库 reactHook)含 自定义hook的方法及使用
  17. 安装linux系统出来7只小企鹅,在Red Hat Linux 9.0下安装小企鹅中文输入法
  18. 测不准原理?记一次Guava队列问题的排查
  19. 伯德图 matlab,matlab画三维伯德图,bode图
  20. 微信小程序开发.小程序入门(上)

热门文章

  1. 大数据时代中数据安全运营面临的主要挑战
  2. win10pin不可用进不去系统_人脸识别门禁控制系统+安检通道
  3. scp命令上传文件到服务器
  4. linux中的xorg进程占用内存资源释放
  5. 华硕笔记本刷BIOS
  6. OpenGL_10 3D空间中移动图像
  7. Word文档太大怎样压缩变小?有没有简单的步骤讲解?
  8. 转1:Python字符编码详解
  9. SIGGRAPH 2018 见闻录
  10. uni-app 在mac电脑连接安卓手机进行真机调试