Android version of the update [transfer]

2010-10-14  来源:本站原创  分类:Mobile  人气:215 

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;
/ **
* Android AutoUpdate.
*
* Lazybone/2010.08.20
*
* 1.Set apkUrl.
*
* 2.check ().
*
* 3.add delFile () method in resume () \ onPause ().
* /
public class MyAutoUpdate {
public Activity activity = null;
public int versionCode = 0;
public String versionName = "";
private static final String TAG = "AutoUpdate";
private String currentFilePath = "";
private String currentTempFilePath = "";
private String fileEx = "";
private String fileNa = "";
private String strURL = "http://127.0.0.1:81/ApiDemos.apk";
private ProgressDialog dialog;
public MyAutoUpdate (Activity activity) {
this.activity = activity;
getCurrentVersion ();
)
public void check () {
if (isNetworkAvailable (this.activity) == false) {
return;
)
if (true) {/ / Check version.
showUpdateDialog ();
)
)
public static boolean isNetworkAvailable (Context ctx) {
try (
ConnectivityManager cm = (ConnectivityManager) ctx
. GetSystemService (Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo ();
return (info! = null & & info.isConnected ());
} Catch (Exception e) {
e.printStackTrace ();
return false;
)
)
public void showUpdateDialog () {
@ SuppressWarnings ("unused")
AlertDialog alert = new AlertDialog.Builder (this.activity)
. SetTitle ("Title")
. SetIcon (R.drawable.icon)
. SetMessage ("Update or not?")
. SetPositiveButton ("Update",
new DialogInterface.OnClickListener () {
public void onClick (DialogInterface dialog,
int which) {
downloadTheFile (strURL);
showWaitDialog ();
)
})
. SetNegativeButton ("Cancel",
new DialogInterface.OnClickListener () {
public void onClick (DialogInterface dialog,
int which) {
dialog.cancel ();
)
}). Show ();
)
public void showWaitDialog () {
dialog = new ProgressDialog (activity);
dialog.setMessage ("Waitting for update ...");
dialog.setIndeterminate (true);
dialog.setCancelable (true);
dialog.show ();
)
public void getCurrentVersion () {
try (
PackageInfo info = activity.getPackageManager (). GetPackageInfo (
activity.getPackageName (), 0);
this.versionCode = info.versionCode;
this.versionName = info.versionName;
} Catch (NameNotFoundException e) {
e.printStackTrace ();
)
)
private void downloadTheFile (final String strPath) {
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;
Runnable r = new Runnable () {
public void run () (
try (
doDownloadTheFile (strPath);
} Catch (Exception e) {
Log.e (TAG, e.getMessage (), e);
)
)
};
new Thread (r). start ();
} Catch (Exception e) {
e.printStackTrace ();
)
)
private void doDownloadTheFile (String strPath) throws Exception {
Log.i (TAG, "getDataSource ()");
if (! URLUtil.isNetworkUrl (strPath)) {
Log.i (TAG, "getDataSource () It's a wrong URL!");
} Else {
URL myURL = new URL (strPath);
URLConnection conn = myURL.openConnection ();
conn.connect ();
InputStream is = conn.getInputStream ();
if (is == null) {
throw new RuntimeException ("stream is null");
)
File myTempFile = File.createTempFile (fileNa, "." + FileEx);
currentTempFilePath = myTempFile.getAbsolutePath ();
FileOutputStream fos = 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 () Download ok ...");
dialog.cancel ();
dialog.dismiss ();
openFile (myTempFile);
try (
is.close ();
} Catch (Exception ex) {
Log.e (TAG, "getDataSource () error:" + ex.getMessage (), ex);
)
)
)
private void openFile (File f) {
Intent intent = new Intent ();
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction (android.content.Intent.ACTION_VIEW);
String type = getMIMEType (f);
intent.setDataAndType (Uri.fromFile (f), type);
activity.startActivity (intent);
)
public void delFile () {
Log.i (TAG, "The TempFile (" + currentTempFilePath + ") was deleted.");
File myFile = new File (currentTempFilePath);
if (myFile.exists ()) {
myFile.delete ();
)
)
private String getMIMEType (File f) {
String type = "";
String fName = f.getName ();
String end = 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;
)
)

相关文章
  • Android version of the update [transfer] 2010-10-14

    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.Prog

  • Android version of video conferencing (for source) 2010-07-05

    This is before the Android version under JavaEye a simple Android application development, purely in practice very hand, if what novice like, please download the code for, not commercial, thank you! Video Conferencing Web Edition: http://t.faqee.com

  • Android version of the video conference 2010-07-05

    This is before the Android version under JavaEye a simple Android application development, purely in practice very hand, if what novice like, please download the code for, not commercial, thank you! Video Conferencing Web Edition: http://t.faqee.com

  • [Transfer] fast switching Android version of the method works 2010-12-15

    android application installation package APK If you are on the high version of the android jar can not be developed in the low version of the android SDK run on, so we need to develop good applications that we have replaced the low back version of th

  • Android under the Eclipse update the upgrade process and resolve problems 2010-07-28

    This article comes from CSDN blog, reproduced, please indicate the source: http://blog.csdn.net/sx1116/archive/2010/06/08/5656378.aspx If you previously used to install android in the sdk, you can Android SDK and AVD Manager (in the eclipse-> Window-

  • Version detection and update 2010-09-15

    Applications can be made automatically, check the latest version, if the need to update the current version of the pop-up dialog box allows users to choose whether to update the current software through the Market. Knowledge Points: SharedPreferences

  • android environment configuration and problem-solving transfer 2010-11-21

    Android development is now in full swing, showing off is not very busy, learning the next, here recorded in the windows in the development environment to build Android on its own is a record entry of new brothers and sisters can refer to it! (1) Inst

  • HTC's Android phone to name the (transfer) 2010-09-09

    Have you ever heard "HTC G1, HTC G2, HTC G3 .... HTC G9" This kind of statement? No, no, this is a wrong copy of a naming system, completely wrong. HTC have never been to "G + number" for the phone models. Only T-Mobile and other carri

  • EditText android version of the monitor (addTextChangedListener) 2010-12-21

    Directly attached to the bar code _ok.setClickable (false); / / initialization not clickable _ok.setEnabled (false); _edit = (EditText) findViewById (R.id.fb_status_eidt); / / Main listener in the implementation of the interface _edit.addTextChangedL

  • How to bulk import the data into android's sqlite database (update) 2010-04-18

    Apr 18, 2010 1. Should use adb push command to copy the file on the hard disk to the android AVD of an application of the databases folder, execute the command as follows: adb push F: \ en.txt data / data / com.wenjie.android.dictionary / databases =

  • <Palm End of the World> Android version 2010-05-08

    Customized for the End of the World Forum android localized interface aspects of the mobile phone to browse, with basic functions, the problem is also lots of .... Support forum here: 1, BBS List 2, message list 3 posts view 4, Collection 5, save the

  • Shocked ~ ~ Fei Liu android version to experience!! 2010-07-01

    Fei Liu android release. Android than the domestic market or other download device compared to support from the chain, really amazing! ! !

  • Shock ~ ~ Fei Liu android version to experience!! 2010-07-01

    Fei Liu android release. Android market compared to domestic or other download device compared to support from the chain, really amazing! ! !

  • The status of domestic and international mobile advertising platform Android version comparison 2010-12-13

    I have four months of entering the android developers, and generally found in the domestic android is currently developing ways to make money to do much, or more limited way I approached, I feel in addition to MM or by uploading to earn a little mone

  • PHP among the high-speed data using the shared memory of a program update [transfer] 2010-12-29

    Of: HonestQiao If you are among the practical applications, you really need high-speed data update operation, then we think you already have the following prerequisites: In accordance with the needs of the application to adjust the server <br /> on

  • android development of reference [update] 2011-05-18

    1. How to set up soft keyboard does not automatically pop up. Method in the activity of oncreate set window of softinputmode. Code: this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 2. Android available to

  • Android version of the accordion (ExpandableListView) 2011-05-25

    ExpandableListView is Android's accordion, I feel effects pretty well. A, ExpandableListView Introduction A vertical scrolling display two levels (Child, Group) view a list of items, list items from ExpandableListAdapter. Groups can be individually s

  • Android Location information access methods (transfer) 2010-06-05

    Number and location in the development of related procedures, often need access to equipment, Location, Location of access equipment as follows: 1. Create a LocationListener interface implementation class, you can use anonymous class, the code is as

  • Android Brush the inner workings of principles of recovery 2010-09-09

    Android version of the update is fast, 2.1 is the most common version, but immediately began to upgrade 2.2 version, and now the Internet provides a lot of self-produced andorid ROM, by Brush Brush tool, the most common is the use of android's recove

  • Android2.2 + Eclipse windows xp environment configuration 2010-08-05

    Under the xp built Android2.2 + Eclipse environment, to process and record the problems, to prepare for future trouble: D 1. Install JDK, First install the JDK, Java SE can present the latest JDK version for the JDK 6 Update 21, download address is: