gpt4 book ai didi

Android - 启动然后在第一次打开时下载

转载 作者:行者123 更新时间:2023-11-30 04:32:55 24 4
gpt4 key购买 nike

我试图在应用程序第一次打开时先显示启动画面,然后转到下载屏幕。在应用程序第一次下载文件后,我希望它从启动画面转到章节菜单屏幕,并且再也不会打开下载器 Activity

我有两个麻烦。

首先。我正在尝试使用 sharedpreferences 来保存下载状态,并使用它来跳过下载程序(如果它以前做过)。我认为我对 sharedpreferences 的理解还不够好

其次。下载进度脚本直接从 0 到 100%。我尝试过使用 Handler,也尝试过使用 runonUI,但我没有运气。

如有任何帮助,我们将不胜感激。我对 java 和 android 很陌生。提前致谢!

这是我的启动图:

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(Bundle 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();
}
}

这是我的下载 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();
}

}

最后,这里是 update.java 的代码:

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;
}

}

最佳答案

您应该使用 AsyncTask 来正确处理下载进度。

您写的是一种偏好,但阅读的是另一种偏好。

关于Android - 启动然后在第一次打开时下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7376178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com