package com.hiyo.game.pdk.tool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;
import android.webkit.URLUtil;
import com.hiyo.game.pdk.activity.R;
/**
*AndroidAutoUpdate.
*
*lazybone/2010.08.20
*
*1.SetapkUrl.
*
*2.check().
*
*3.adddelFile()methodinresume()\onPause().
*/
public class MyAutoUpdate{
public Activityactivity = null ;
public int versionCode = 0 ;
public StringversionName = "" ;
private static final StringTAG = " AutoUpdate " ;
private StringcurrentFilePath = "" ;
private StringcurrentTempFilePath = "" ;
private StringfileEx = "" ;
private StringfileNa = "" ;
private StringstrURL = " http://127.0.0.1:81/ApiDemos.apk " ;
private ProgressDialogdialog;
public MyAutoUpdate(Activityactivity){
this .activity = activity;
getCurrentVersion();
}
public void check(){
if (isNetworkAvailable( this .activity) == false ){
return ;
}
if ( true ){ // Checkversion.
showUpdateDialog();
}
}
public static boolean isNetworkAvailable(Contextctx){
try {
ConnectivityManagercm
= (ConnectivityManager)ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfoinfo
= cm.getActiveNetworkInfo();
return (info != null && info.isConnected());
}
catch (Exceptione){
e.printStackTrace();
return false ;
}
}
public void showUpdateDialog(){
@SuppressWarnings(
" unused " )
AlertDialogalert
= new AlertDialog.Builder( this .activity)
.setTitle(
" Title " )
.setIcon(R.drawable.icon)
.setMessage(
" Updateornot? " )
.setPositiveButton(
" Update " ,
new DialogInterface.OnClickListener(){
public void onClick(DialogInterfacedialog,
int which){
downloadTheFile(strURL);
showWaitDialog();
}
})
.setNegativeButton(
" Cancel " ,
new DialogInterface.OnClickListener(){
public void onClick(DialogInterfacedialog,
int which){
dialog.cancel();
}
}).show();
}
public void showWaitDialog(){
dialog
= new ProgressDialog(activity);
dialog.setMessage(
" Waittingforupdate... " );
dialog.setIndeterminate(
true );
dialog.setCancelable(
true );
dialog.show();
}
public void getCurrentVersion(){
try {
PackageInfoinfo
= activity.getPackageManager().getPackageInfo(
activity.getPackageName(),
0 );
this .versionCode = info.versionCode;
this .versionName = info.versionName;
}
catch (NameNotFoundExceptione){
e.printStackTrace();
}
}
private void downloadTheFile( final StringstrPath){
fileEx
= strURL.substring(strURL.lastIndexOf( " . " ) + 1 ,strURL.length())
.toLowerCase();
fileNa
= strURL.substring(strURL.lastIndexOf( " / " ) + 1 ,
strURL.lastIndexOf(
" . " ));
try {
if (strPath.equals(currentFilePath)){
doDownloadTheFile(strPath);
}
currentFilePath
= strPath;
Runnabler
= new Runnable(){
public void run(){
try {
doDownloadTheFile(strPath);
}
catch (Exceptione){
Log.e(TAG,e.getMessage(),e);
}
}
};
new Thread(r).start();
}
catch (Exceptione){
e.printStackTrace();
}
}
private void doDownloadTheFile(StringstrPath) throws Exception{
Log.i(TAG,
" getDataSource() " );
if ( ! URLUtil.isNetworkUrl(strPath)){
Log.i(TAG,
" getDataSource()It'sawrongURL! " );
}
else {
URLmyURL
= new URL(strPath);
URLConnectionconn
= myURL.openConnection();
conn.connect();
InputStreamis
= conn.getInputStream();
if (is == null ){
throw new RuntimeException( " streamisnull " );
}
FilemyTempFile
= File.createTempFile(fileNa, " . " + fileEx);
currentTempFilePath
= myTempFile.getAbsolutePath();
FileOutputStreamfos
= new FileOutputStream(myTempFile);
byte buf[] = new byte [ 128 ];
do {
int numread = is.read(buf);
if (numread <= 0 ){
break ;
}
fos.write(buf,
0 ,numread);
}
while ( true );
Log.i(TAG,
" getDataSource()Downloadok... " );
dialog.cancel();
dialog.dismiss();
openFile(myTempFile);
try {
is.close();
}
catch (Exceptionex){
Log.e(TAG,
" getDataSource()error: " + ex.getMessage(),ex);
}
}
}
private void openFile(Filef){
Intentintent
= new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
Stringtype
= getMIMEType(f);
intent.setDataAndType(Uri.fromFile(f),type);
activity.startActivity(intent);
}
public void delFile(){
Log.i(TAG,
" TheTempFile( " + currentTempFilePath + " )wasdeleted. " );
FilemyFile
= new File(currentTempFilePath);
if (myFile.exists()){
myFile.delete();
}
}
private StringgetMIMEType(Filef){
Stringtype
= "" ;
StringfName
= f.getName();
Stringend
= fName
.substring(fName.lastIndexOf(
" . " ) + 1 ,fName.length())
.toLowerCase();
if (end.equals( " m4a " ) || end.equals( " mp3 " ) || end.equals( " mid " )
|| end.equals( " xmf " ) || end.equals( " ogg " ) || end.equals( " wav " )){
type
= " audio " ;
}
else if (end.equals( " 3gp " ) || end.equals( " mp4 " )){
type
= " video " ;
}
else if (end.equals( " jpg " ) || end.equals( " gif " ) || end.equals( " png " )
|| end.equals( " jpeg " ) || end.equals( " bmp " )){
type
= " image " ;
}
else if (end.equals( " apk " )){
type
= " application/vnd.android.package-archive " ;
}
else {
type
= " * " ;
}
if (end.equals( " apk " )){
}
else {
type
+= " /* " ;
}
return type;
}
}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 再谈文件读写:判断文件的几种方法及其优劣
  2. 从一篇Blog看两个并发编程错误
  3. 每日前端夜话(0x04):2018年JavaScript状态
  4. LeetCode #27 移除元素
  5. 以B站C语言视频为基础的课后总结(一)
  6. LeetCode #26 删除排序数组中的重复项
  7. 排序算法 #5 归并排序
  8. 来自Kenneth Reitz大神的建议:避免不必要
  9. 满满的一篇,全是复杂度分析核心知识点
  10. LeetCode #80 删除排序数组中的重复项II