android新版本怎么下载安装,android 新版本下载 安装
1 首先在程序啟動的時候 向服務器發送請求 服務器會返回??? String? newVerString="2|app.quannaojiaoyu.com......"
2 然后根據 “|”拆分為倆個list 元素?? 取第一個list 元素 和當前的版本號進行比較 如果此元素大于版本號 則發現新版本開始下載 url 為list的第二個元素? 下載完畢即可安裝
String newVerString = functions.getHttpResponse(
urlApi + "base.asp", "getVer");
System.out.println("newString--->"+newVerString);
Log.d("StartPage", "Check Server.");
if (newVerString != null)
{
ArrayList listVerStrings = functions.splitString(newVerString);
//得到當前版本號
int curVersion = functions.getAppVersionName(getApplicationContext());
Log.d("StartPage", listVerStrings.get(0));
// Check New Version
if (curVersion < Integer.parseInt(listVerStrings.get(0)))
{
Log.d("StartPage", "find new version.");
try
{
URL Url = new URL(listVerStrings.get(1));
URLConnection conn = Url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
int fileSize = conn.getContentLength();//獲取文件長度
Log.d("StartPage", "new version download start ("
+ String.valueOf(fileSize) + ")...");
msg = new Message();
msg.what = UPDATE_NEWVER;
msg.arg1 = fileSize;
handler.sendMessage(msg);
if (fileSize <= 0)return;
FileOutputStream FOS = new FileOutputStream(strSDPath
+ "/cawords.apk");
byte buf[] = new byte[1024];
int downLoadFilePosition = 0;
int numread;
while ((numread = is.read(buf)) != -1)
{
FOS.write(buf, 0, numread);
downLoadFilePosition += numread;
Log.d("StartPage", "new version downloading ("
+ String.valueOf(downLoadFilePosition)
+ ")...");
msg = new Message();
msg.what = UPDATE_DOWNLOAD;
msg.arg1 = downLoadFilePosition;
handler.sendMessage(msg);
}
Log.d("StartPage", "New Version Downloaded.");
Intent intent = new Intent();
// 安裝程序
intent.setDataAndType(
Uri.fromFile(new File(strSDPath
+ "/com.quannaojiaoyu.cadict.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
Log.d("StartPage", "New Version Installed.");
finish();
System.exit(0);
} catch (Exception ex)
{
如何得到當前版本號:
public static int getAppVersionName(Context context) { ? int versionName = -1; ? try ? { ?? PackageManager pm = context.getPackageManager(); ?? PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0); ?? versionName = pi.versionCode; ?? Log.d("versionName ", versionName +""); ? } catch (Exception e) ? { ?? Log.e("GET VER", "Exception", e); ? } ? return versionName; }
總結
以上是生活随笔為你收集整理的android新版本怎么下载安装,android 新版本下载 安装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拾方易公众号运营成本低么?
- 下一篇: 数独如何保证唯一解