Android版本检测\自动更新

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); 运行此发放报错

Uri解析出的sd卡file的路径为/sdcard/xxx.apk 在android搜索程序打开时找不到相关的Activity.

需要添加”file://"头.

intent.setDataAndType(Uri.parse("file://"+f.getPath(),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;
}
}

另一种版本安装方式

market://search?q=pub:yale

直接发送安装路径或者market://search?pid=xxxx"路径交给market或者下载管理器去下载,安装需要用户点击安装文件安装.

Uri uri = Uri.parse("http://192.168.1.121:8085/RadioCom_Release1.1_C.apk");
Intent intent = new Intent(
Intent.ACTION_VIEW, uri);
startActivity(intent);

你可能感兴趣的:(thread,android,.net,F#,webkit)