开发者

Android - splash then download on first opening

I'm trying to have a splash screen then move to a downloading screen the first time the app is ever opened. after the app downloads the files the very first time, i want it to go from the splash to the chaptermenu screen and never open the downloader activity ever again

I'm having two troubles.

First. I am trying to use sharedpreferences to make it save the download status and use that to skip the downloader if it has ever done it before. I don't think i understand sharedpreferences well enough just yet

Second. The download progress script goes straight from 0 to 100%. I have tried playing around with a Handler and I've also tried using runonUI but am having no luck.

Any help would be appreciated. I'm quite new to java and android. Thanks in advance!

Here is my splash:

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

public class splashscreen extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bu开发者_如何学编程ndle SavedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(SavedInstanceState);
    setContentView(R.layout.splashlayout);
    SharedPreferences myPrefs = this.getSharedPreferences("DOWNLOAD_STATUS", MODE_WORLD_READABLE);
    Update.storeDownloadStatusString(myPrefs.getString("DOWNLOAD_STATUS", "NotDownloaded"));
    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(5000);
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
            finally{
                Intent checkDownloadIntentObject = new Intent("blah.blah.CHECKDOWNLOAD");
                Intent chapterMenuObject = new Intent("blah.blah.CHAPTERMENU");
                if (Update.getDownloadStatusString()=="NotDownloaded"){
                    startActivity(checkDownloadIntentObject);
                }else{
                    startActivity(chapterMenuObject);
                }
            }
        }

    };  
    timer.start();

    }

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
}

And here is my downloader activity:

public class CheckDownload extends Activity {
    public static int downloadedSize;
    public static int totalSize;

// Called when the activity is first created. 
@Override
protected void onCreate(Bundle SavedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(SavedInstanceState);
    setContentView(R.layout.downloadscreen);
    SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
    final SharedPreferences.Editor prefsEditor = myPrefs.edit();
    Thread downloader = new Thread() {
        public void run() {
            try {
                // set the download URL, a url that points to a file on the
                // internet
                // this is the file to be downloaded
                URL url = new URL(
                        "http://insert.url.here.blah");

                // create the new connection
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();

                // set up some things on the connection
                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);

                // and connect!
                urlConnection.connect();

                // set the path where we want to save the file
                // in this case, going to save it on the root directory of
                // the
                // sd card.

                File folderdestination = new File(
                        Environment.getExternalStorageDirectory()
                                + "/blah");
                folderdestination.mkdir();
                File file = new File(folderdestination, "blah.zip");

                // create a new file, specifying the path, and the filename
                // which we want to save the file as.
                String fileurl = file.getAbsolutePath();

                // this will be used to write the downloaded data into the
                // file we created
                FileOutputStream fileOutput = new FileOutputStream(file);

                // this will be used in reading the data from the internet
                InputStream inputStream = urlConnection.getInputStream();

                // this is the total size of the file
                int totalSize = urlConnection.getContentLength();
                // variable to store total downloaded bytes
                int downloadedSize = 0;

                // create a buffer...
                byte[] buffer = new byte[1024];
                int bufferLength = 0; // used to store a temporary size of
                                        // the buffer

                // now, read through the input buffer and write the contents
                // to the file
                while ((bufferLength = inputStream.read(buffer)) > 0) {
                    // add the data in the buffer to the file in the file
                    // output stream (the file on the sd card
                    fileOutput.write(buffer, 0, bufferLength);
                    // add up the size so we know how much is downloaded
                    downloadedSize += bufferLength;
                    // this is where you would do something to report the
                    // prgress, like this maybe
                    Update.setDS(downloadedSize);
                    Update.setTS(totalSize);
                    updateProgress();

                }
                // close the output stream when done
                fileOutput.close();
                Unzipper.unzip(fileurl);
                // catch some possible errors...
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                Intent chapterMenuIntentObject = new Intent(
                        "blah.blah.CHAPTERMENU");
                prefsEditor.putString("DOWNLOAD_STATUS", "Downloaded");
                prefsEditor.commit();
                startActivity(chapterMenuIntentObject);
            }

        }

    };
    downloader.start();

}

protected void updateProgress() {
    runOnUiThread(new Runnable() {
        public void run() {
            int dS=Update.getDS();
            int tS=Update.getTS();
            float percentage = dS / tS * 100;
            String stringy = "Downloading!\n\n" +
                    "This will only happen the first time you open this application.\n\n" +
                    "This may take several minutes. Please be patient.\n\n" +
                    "Download Progress: " + percentage + "%";
            TextView textytext = (TextView) findViewById(R.id.downloadscreentextview);
            textytext.setText(stringy);
        }
    });
}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

Lastly, here is update.java 's code:

public class Update{
public static int dsize;
public static int tsize;
public static String downloadStatusString;
public static void setDS(int ds){
    dsize=ds;
}
public static int getDS(){
    return dsize;
}
public static void setTS(int ts){
    tsize=ts;
}
public static int getTS(){
    return tsize;
}
public static void storeDownloadStatusString(String downloadStatus){
    downloadStatusString=downloadStatus;
}
public static String getDownloadStatusString(){
    return downloadStatusString;
}

}


You should be using AsyncTask to handle the download with progress correctly.

You are writting to one preference but reading from a different one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜