最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
android應(yīng)用升級模塊解析

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

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

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

        假定當(dāng)前為第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() {

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

int vc = getVersionCode();

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

int newVc = 2;//假定從后臺獲取的版本為第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("更新升級")

.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);

//完善通知

//設(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)的頁面

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)用升級模塊解析

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

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


標(biāo)題名稱:android應(yīng)用升級模塊解析
網(wǎng)站地址:http://fisionsoft.com.cn/article/ihjdcc.html