標籤:android 應用升級
一共就用到一個類:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/57/41/wKiom1SWa3_jppJIAAB2XuKLomY692.jpg" title="main.png" alt="wKiom1SWa3_jppJIAAB2XuKLomY692.jpg" /> 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/57/41/wKiom1SWa-rjrUh2AAEjK-uVF2c912.jpg" title="code.jpg" alt="wKiom1SWa-rjrUh2AAEjK-uVF2c912.jpg" />
假定當前為第1版,通過對背景請求,收到第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() {
//擷取當前應用版本號碼
int vc = getVersionCode();
/*通常每次開啟app都會向後台請求一個版本號碼,若與目前的版本相同,則不提醒,若大於目前的版本,則提示升級*/
int newVc = 2;//假定從後台擷取的版本為第2版,大於目前的版本
final String apkUrl = "http://meishipic.qiniudn.com/2010052615045178.jpg";//apk,暫用一張圖片代替
String apkMessage = "1、增加了XXX \n2、修改了XXX"; //增加的新特性
if(vc < newVc)
{
new AlertDialog.Builder(this).setTitle("更新升級")
.setMessage(apkMessage)
.setPositiveButton("升級", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//升級
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();//獲得檔案長度
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);
//完善通知
//設定點擊通知安裝
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, "更新完成,點擊安裝");
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);//點擊設定跳轉的頁面
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("標題1").setContent(remoteViews).setContentText("內容1")
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent).
build();
manager.notify(NOTIFY_ID, notification);
}
}
這樣就實現了app檢查版本提示更新的功能。許可權不要漏了。
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/57/41/wKiom1SWbaHyhCg4AAHAZtrXSZg957.jpg" style="float:none;" title="模擬器1.jpg" alt="wKiom1SWbaHyhCg4AAHAZtrXSZg957.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/57/3E/wKioL1SWbkrgCAweAAGOYEONcAg517.jpg" style="float:none;" title="模擬器2.jpg" alt="wKioL1SWbkrgCAweAAGOYEONcAg517.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/57/41/wKiom1SWbaXT_2VVAAGUmmbQPQM478.jpg" style="float:none;" title="模擬器3.jpg" alt="wKiom1SWbaXT_2VVAAGUmmbQPQM478.jpg" />
本文出自 “破公子的偽技術部落格” 部落格,請務必保留此出處http://pogongzi.blog.51cto.com/8982426/1592282
android 應用升級模組解析