android應(yīng)用升級(jí)模塊解析

     一共就用到一個(gè)類:

發(fā)展壯大離不開(kāi)廣大客戶長(zhǎng)期以來(lái)的信賴與支持,我們將始終秉承“誠(chéng)信為本、服務(wù)至上”的服務(wù)理念,堅(jiān)持“二合一”的優(yōu)良服務(wù)模式,真誠(chéng)服務(wù)每家企業(yè),認(rèn)真做好每個(gè)細(xì)節(jié),不斷完善自我,成就企業(yè),實(shí)現(xiàn)共贏。行業(yè)涉及成都咖啡廳設(shè)計(jì)等,在成都網(wǎng)站建設(shè)、成都營(yíng)銷網(wǎng)站建設(shè)、WAP手機(jī)網(wǎng)站、VI設(shè)計(jì)、軟件開(kāi)發(fā)等項(xiàng)目上具有豐富的設(shè)計(jì)經(jīng)驗(yàn)。

android 應(yīng)用升級(jí)模塊解析 android 應(yīng)用升級(jí)模塊解析

        假定當(dāng)前為第1版,通過(guò)對(duì)后臺(tái)的請(qǐng)求,收到第2版的更新通知。代碼如:

package com.example.ex_templete;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import android.net.Uri;

import android.os.AsyncTask;

import android.os.Bundle;

import android.os.Environment;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.pm.PackageInfo;

import android.content.pm.PackageManager;

import android.content.pm.PackageManager.NameNotFoundException;

import android.support.v4.app.NotificationCompat;

import android.view.Menu;

import android.view.View;

import android.widget.RemoteViews;

public class MainActivity extends Activity {

private static final String PATH_APK_UPGROUP = Environment.getExternalStorageDirectory()

  + "/myapp/download/myapp.apk";

private static final int NOTIFY_ID = 1234;

private RemoteViews remoteViews;

private Notification notification;

private NotificationManager manager;

private int fileLen;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        autoupgrade();

    }

    private void autoupgrade() {

    //獲取當(dāng)前應(yīng)用版本號(hào)

int vc = getVersionCode();

/*通常每次打開(kāi)app都會(huì)向后臺(tái)請(qǐng)求一個(gè)版本號(hào),若與當(dāng)前版本相同,則不提醒,若大于當(dāng)前版本,則提示升級(jí)*/

int newVc = 2;//假定從后臺(tái)獲取的版本為第2版,大于當(dāng)前版本

final String apkUrl = "http://meishipic.qiniudn.com/2010052615045178.jpg";//apk下載地址,暫用一張圖片代替

String apkMessage = "1、增加了XXX \n2、修改了XXX";  //增加的新特性

if(vc < newVc)

{

new AlertDialog.Builder(this).setTitle("更新升級(jí)")

.setMessage(apkMessage)

.setPositiveButton("升級(jí)", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

 //升級(jí)

 new MyTask().execute(apkUrl);

}

}).setNegativeButton("取消", null).create().show();

}

}

    

    class MyTask extends AsyncTask<String, Integer, Boolean>

    {

    @Override

    protected void onPreExecute() {

    super.onPreExecute();

    //通知

    showNotifi();

    }

@Override

protected Boolean doInBackground(String... params) {

//下載apk

InputStream is = null;

FileOutputStream fos = null;

try {

URL url = new URL(params[0]);

HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();

fileLen = openConnection.getContentLength();//獲得文件長(zhǎng)度

int responseCode = openConnection.getResponseCode();

if(responseCode != HttpURLConnection.HTTP_OK)

{

//提示

return null;

}

//判斷文件路徑是否存在

File file =new File(PATH_APK_UPGROUP);

if(!file.getParentFile().exists())

{

file.getParentFile().mkdirs();

}

is = openConnection.getInputStream();

int len = 0;

byte[] buffer = new byte[1024];

//已下載大小

int loadLen = 0;

fos = new FileOutputStream(PATH_APK_UPGROUP);

int num = 1;

while(-1 != (len = is.read(buffer)))

{

//Thread.sleep(100);

fos.write(buffer, 0, len);

loadLen += len;

if(loadLen *100 / fileLen >= 1*num)

{

num++;

publishProgress(loadLen);

}

}

fos.flush();

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally{

if(is != null)

{

try {

is.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(fos != null)

{

try {

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return null;

}

@Override

protected void onProgressUpdate(Integer... values) {

super.onProgressUpdate(values);

//更新通知

int len = values[0];

notification.contentView.setTextViewText(R.id.tv12, "更新了"+ len*100/fileLen +"%");

notification.contentView.setProgressBar(R.id.progressBar1, fileLen, len, false);

manager.notify(NOTIFY_ID, notification);

}

@Override

protected void onPostExecute(Boolean result) {

super.onPostExecute(result);

//完善通知

//設(shè)置點(diǎn)擊通知安裝

Intent intent = new Intent(Intent.

ACTION_VIEW);

intent.setDataAndType(Uri.parse("file://"+

PATH_APK_UPGROUP),

"application/vnd.android."

+ "package-archive");

notification.contentView.setTextViewText(R.id.tv12, "更新完成,點(diǎn)擊安裝");

notification.contentView.setViewVisibility(R.id.progressBar1, View.GONE);

PendingIntent ic = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

notification.contentIntent = ic;

manager.notify(NOTIFY_ID, notification);

//startActivity(intent);

}

   

    }

private int getVersionCode() {

PackageManager manager = getPackageManager();

try {

PackageInfo packageInfo = 

manager.getPackageInfo(getPackageName(), 0);

return packageInfo.versionCode;

} catch (NameNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return 0;

}

public void showNotifi() {

manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent = new Intent();

intent.setClass(this, MainActivity.class);//點(diǎn)擊設(shè)置跳轉(zhuǎn)的頁(yè)面

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,

intent, 0);

remoteViews = new 

RemoteViews(getPackageName(), R.layout.notification);

notification = 

new NotificationCompat.Builder(this)

.setSmallIcon(R.drawable.ic_launcher).setContentTitle("通知")

.setTicker("標(biāo)題1").setContent(remoteViews).setContentText("內(nèi)容1")

.setWhen(System.currentTimeMillis())

.setContentIntent(contentIntent).

build();

manager.notify(NOTIFY_ID, notification);

}

    

}

    這樣就實(shí)現(xiàn)了app檢查版本提示更新的功能。權(quán)限不要漏了。

    

android 應(yīng)用升級(jí)模塊解析

android 應(yīng)用升級(jí)模塊解析

android 應(yīng)用升級(jí)模塊解析

文章題目:android應(yīng)用升級(jí)模塊解析
瀏覽地址:http://muchs.cn/article22/ihjdcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化Google、品牌網(wǎng)站制作、服務(wù)器托管網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)