成都创新互联网站制作重庆分公司

android应用升级模块解析

     一共就用到一个类:

发展壮大离不开广大客户长期以来的信赖与支持,我们将始终秉承“诚信为本、服务至上”的服务理念,坚持“二合一”的优良服务模式,真诚服务每家企业,认真做好每个细节,不断完善自我,成就企业,实现共赢。行业涉及成都咖啡厅设计等,在成都网站建设成都营销网站建设、WAP手机网站、VI设计、软件开发等项目上具有丰富的设计经验。

android 应用升级模块解析 android 应用升级模块解析

        假定当前为第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

    {

    @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检查版本提示更新的功能。权限不要漏了。

    

android 应用升级模块解析

android 应用升级模块解析

android 应用升级模块解析


网站标题:android应用升级模块解析
分享网址:http://cxhlcq.com/article/ihjdcc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部