温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android 应用升级模块解析

发布时间:2020-06-29 23:52:32 来源:网络 阅读:775 作者:奇迹兄 栏目:移动开发

     一共就用到一个类:

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


    

android 应用升级模块解析

android 应用升级模块解析

android 应用升级模块解析



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI