gpt4 book ai didi

Android-下载文件+状态栏通知拖慢手机速度

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:03:41 25 4
gpt4 key购买 nike

我目前有一个 asynctask 从服务器下载 mp3。当用户开始下载它时,会创建一个状态栏通知。这会实时显示下载进度。我唯一担心的是手机速度变慢了太多。有什么方法可以延迟显示的进度或使我的代码更快?谢谢。

代码如下:

public class DownloadFile extends AsyncTask<String, String, String> {
CharSequence contentText;
Context context;
CharSequence contentTitle;
PendingIntent contentIntent;
int HELLO_ID = 1;
long time;
int icon;
CharSequence tickerText;
File file;

public void downloadNotification() {
String ns = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager) getSystemService(ns);

icon = R.drawable.sdricontest;
//the text that appears first on the status bar
tickerText = "Downloading...";
time = System.currentTimeMillis();

notification = new Notification(icon, tickerText, time);

context = getApplicationContext();
//the bold font
contentTitle = "Your download is in progress";
//the text that needs to change
contentText = "0% complete";
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setType("audio/*");
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(HELLO_ID, notification);
}

@Override
protected void onPreExecute() {
//execute the status bar notification
downloadNotification();
super.onPreExecute();
}

@Override
protected String doInBackground(String... url) {
int count;
try {
URL url2 = new URL(sdrUrl);
HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();

int lengthOfFile = connection.getContentLength();

//make the stop drop rave folder
File sdrFolder = new File(Environment.getExternalStorageDirectory() + "/StopDropRave");
boolean success = false;

if (!sdrFolder.exists()) {
success = sdrFolder.mkdir();
}
if (!success) {
String PATH = Environment.getExternalStorageDirectory()
+ "/StopDropRave/";
file = new File(PATH);
file.mkdirs();
} else {
String PATH = Environment.getExternalStorageDirectory()
+ "/StopDropRave/";
file = new File(PATH);
file.mkdirs();
}

String[] path = url2.getPath().split("/");
String mp3 = path[path.length - 1];
String mp31 = mp3.replace("%20", " ");
String sdrMp3 = mp31.replace("%28", "(");
String sdrMp31 = sdrMp3.replace("%29", ")");
String sdrMp32 = sdrMp31.replace("%27", "'");

File outputFile = new File(file, sdrMp32);
FileOutputStream fos = new FileOutputStream(outputFile);

InputStream input = connection.getInputStream();

byte[] data = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) (total * 100 / lengthOfFile));
fos.write(data, 0, count);
}
fos.close();
input.close();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
public void onProgressUpdate(String... progress) {
contentText = Integer.parseInt(progress[0]) + "% complete";
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(HELLO_ID, notification);
super.onProgressUpdate(progress);
}
}

最佳答案

我看到了类似的结果,你不需要那么频繁地推送更新通知,我把我的更新改为每秒只更新几次。 (例如,在 onProgressUpdate 中跟踪您上次调用 notify 的时间,并且仅当您超过上次调用的 100 毫秒,或者您处于最大值时才调用通知。

关于Android-下载文件+状态栏通知拖慢手机速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6929323/

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