Automatic Updates client code for Android

2011-01-01  来源:本站原创  分类:Java  人气:143 

import java.io.File;  

import java.io.FileOutputStream;

import java.io.IOException;  

import java.io.InputStream;  

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;  

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;  

public class Update extends BaseActivity {
    public ProgressDialog pBar;
    private Handler handler = new Handler();  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update);
        Dialog dialog = new AlertDialog.Builder(Update.this).setTitle(" System update ")
                .setMessage(" Discover the new version, please update !")//  Set the content
                .setPositiveButton(" OK,//set the OK button
                        new DialogInterface.OnClickListener() {  

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                pBar = new ProgressDialog(Update.this);
                                pBar.setTitle(" Downloading ");
                                pBar.setMessage(" Please wait while the ...");
                                pBar
                                        .setProgressStyle(ProgressDialog.STYLE_SPINNER);
                                downFile("http://url:8765/OA.apk");   

                            }  

                        }).setNegativeButton(" Cancel ",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                //  Click on the "Cancel " Button to exit the program after   

                            }
                        }).create();//  Create
        //  Displays a dialog box
        dialog.show();  

    }  

    void downFile(final String url) {
        pBar.show();
        new Thread() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                // params[0] Represents the connection url
                HttpGet get = new HttpGet(url);
                HttpResponse response;
                try {
                    response = client.execute(get);
                    HttpEntity entity = response.getEntity();
                    long length = entity.getContentLength();
                    InputStream is = entity.getContent();
                    FileOutputStream fileOutputStream = null;
                    if (is != null) {  

                        File file = new File(Environment
                                .getExternalStorageDirectory(), "OA.apk");
                        fileOutputStream = new FileOutputStream(file);  

                        byte[] buf = new byte[1024];
                        int ch = -1;
                        int count = 0;
                        while ((ch = is.read(buf)) != -1) {
                            // baos.write(buf, 0, ch);
                            fileOutputStream.write(buf, 0, ch);
                            count += ch;
                            if (length > 0) {  

                            }  

                        }  

                    }
                    fileOutputStream.flush();
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                    down();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }  

        }.start();  

    }  

    void down() {
        handler.post(new Runnable() {
            public void run() {
                pBar.cancel();
                update();
            }
        });  

    }  

    void update() {  

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File("/sdcard/OA.apk")),
                "application/vnd.android.package-archive");
        startActivity(intent);
    }  

}
相关文章
  • Automatic Updates client code for Android 2011-01-01

    import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.ap

  • B / S and C / S architecture, integration, software, client-side through the WebService interface, automatic updates and upload the data to achieve 2010-03-29

    B / S and C / S architecture - software to achieve the client through the WebService interface, automatic updates and upload data to support any client-side language environments. Test case: Open Client to automatically download the update file, uplo

  • Update content with automatic updates prompted an implementation 2011-01-06

    Ideas are as follows: 1. To implement a program only after the installation only when you first open implementation of the method, so easy in the configuration file PreferenceManager.getDefaultSharedPreferences (Context) some of the information store

  • Spring library to support the unified deployment and automatic updates 2010-04-29

    Cause In traditional projects, the project required libraries are deployed in the run-time environment lib directory, if you want to update a library Jar, you need to replace all of the runtime environment of the lib directory If the project is only

  • Close MyEclipse7.0 automatic updates + Optimal Startup 2010-12-03

    Close MyEclipse7.0 start automatically updated + optimization 1. Window -> preferences -> General -> Startup and Shutdown -> in the list to find "Automatic Updates Scheduler" item to remove the front hook. 2. Window -> Preferences

  • Turn off automatic updates Eclipse 2010-02-03

    In the development process, open the Eclipse update often with automatic pop-up dialog box, even in Windows->; preferences is already turned off automatic updates option will still pop-up dialog box when you open the Eclipse, we can try to clear the

  • Using Cygwin on Windows, to obtain the source code for Android 2010-03-29

    Using Cygwin on Windows, to obtain the source code for Android 1 in preparation for Cygwin environment, which should curl, wget, python and other basic tools. 2, ready to store source code directory (eg: c: \ myeclair), into the Cygwin Shell environm

  • MyEclipse 8.x turn off automatic updates 2010-03-29

    1. Window -> Preferences -> General -> Startup and Shutdown -> inside the list item to find "Automatic Updates Scheduler" item to remove the previous hook. (Note: To list box below to find inside a little by the following) 2. Window

  • The source code to Android SDK 2010-03-14

    When you debug Android program encountered "source not found" error it should be very crazy, Goolge in the SDK did not release the source code included into the event, rather perplexing to many people, Git is a jerky thing no doubt, but framewor

  • Remove Myeclipse7.0 Automatic Updates 2010-05-11

    Recently installed a Myeclipse7.0, the result of a frantic opening it to update, makes it is annoying! Close Myeclipse7.0 is provided for automatic updates: 1. Window -> preferences -> General -> Startup and Shutdown -> in the list, find "

  • Axis2 SOAP may use (1) create client code 2010-07-20

    Axis2 SOAP may use (1) create client code Home http://ws.apache.org/axis2/ create client guide http://ws.apache.org/axis2/1_5_1/userguide-creatingclients.html # createclients Copy the downloaded axis2 to working directory, configuration, AXIS2_HOME =

  • How to write efficient code for Android 2010-09-09

    Modern handheld devices, so much the phone, more like a computer in your hand. However, even the "fastest" of the handheld device, its performance is also not keep up with an ordinary desktop computers. That is why we write Android applications

  • Myeclipse 8.0 Automatic Updates Close 2010-09-15

    1.window-> Perferences-> General-> Startup and Shutdown tick off Automatic Updates Scheduler 2.window-> Perferences-> MyEclipse-> Maven4MyEclipse -> Maven tick off Download repository index updates on startup 3.window-> Perferences

  • git source code for Android 2010-09-25

    git source code for Android: 1. git clone git://android.git.kernel.org/ + project path. 2. Mkdir mydroid cd mydroid repo init-u git: / / android.git.kernel.org / platform / manifest.git repo sync git clone for the larger source, it can not HTTP, more

  • xfire related services, distribution and generation of client code 2010-10-13

    Posted webservice service and generate client code using Eclipse released the first service and a way to generate a client code, released webservice services: 1, New Project Eclipse, the new "dynamic web project" 2, the package will increase the

  • Turn off Automatic Updates after the lower right corner to prevent prompted a security alert? 2010-11-26

    How to turn off windows security alerts and automatic updates Start - Control Panel - Administrative Tools - Services-security center. Double-click the service, open the properties settings. The startup type is set to "Disabled", and then click

  • MyEclipse 7.5 How to turn off automatic updates 2011-01-09

    Off-line collection of MyEclipse 7.5 automatic update methods, record. (1) Turn off automatic updates maven: window-preferences-MyEclipse Enterprise Workbench-Maven4MyEclipse-Maven, Download and Update at the beginning of closing all options, a total

  • The Java source code for Android and Eclipse View the latest methods of Association 2011-05-24

    Of: lizongbo Posted: 01:59. Saturday, January 1st, 2011 Copyright : free to reprint, reprint, sure to articles marked hyperlink original source and author information and this copyright notice. http://618119.com/archives/2011/01/01/201.html In the Go

  • Turn off Automatic Updates after the lower right corner of tips on how to prevent security alerts? 2010-11-26

    How to turn off windows security alerts and automatic updates Start - Control Panel - Administrative Tools - Services-security center. Double-click the service, open the property. The startup type to "Disabled", then under Service Status click t

  • Your computer may be at risk - Automatic Updates is turned off - Click this balloon fix this problem. " 2011-03-04

    Our system will sometimes appear in the lower right corner "Your computer may be at risk - Automatic Updates is turned off - Click this balloon fix this problem" message, then when we encounter this prompt how to do it? This is a genuine WIN XP